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
 General SQL Server Forums
 New to SQL Server Programming
 varchar to datetime

Author  Topic 

mvanwyk
Yak Posting Veteran

99 Posts

Posted - 2009-08-12 : 08:09:55
Hi Guys.

I'm sitting with a issue that i'm not sure how to fix.

One of my Clients tables has a specific field call CALL_TIME varchar (6)DDMMYY for example todays date 120809.

Is it possible to still use the DATEPART function with this field or do i need to convert it somehow?

Any assistance would be appreciated.

Regards.

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-08-12 : 08:40:44
You'd need to do something like:

declare @dt varchar(6), @dt2 datetime

set @dt = '120809'

select @dt2 = convert(datetime,substring(@dt,5,2) + substring(@dt,3,2) + substring(@dt,1,2))

select datepart(dd,@dt2)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-12 : 09:49:23
If the format is same,

declare @dt varchar(6), @dt2 datetime

set @dt = '120809'

select LEFT(@dt,2) as day,substring(@dt,3,2) as month,right(@dt,2) as year


Madhivanan

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

- Advertisement -