Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Convert nvarchar to datetime

Author  Topic 

calpolyjeff
Starting Member

3 Posts

Posted - 2010-01-05 : 17:58:45
An alphanumeric ID Number that I have in my ADP/SQL database contains within
it 6 characters representing the birth date. So of you were born today, your
ID would include 010510, as in mmddyy.

I can extract those 6 digits using SUBSTRING(), but I can't seem to get
them to convert to a date data type (so I can test for the age of the
person). As an experiment, I used a today's date in this expression:

CONVERT (DATETIME, '010510')

and got "May 10 2001 12:00AM" as the output. Obviously, the function did
not read my string as mmddyy, but as yymmdd. What I want is for the output to
be "01/05/10." Is this possible?

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-01-05 : 19:08:43
SET DATEFORMAT mdy


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-06 : 01:26:25
Use one of the methods

declare @date varchar(8)
set @date='010510'
select convert(datetime,stuff(stuff(@date,3,0,'-'),6,0,'-'))
select cast(stuff(@date,1,4,left(year(getdate()),2))+left(@date,4) as datetime)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -