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
 Converting varchar to datetime

Author  Topic 

DaveBF
Yak Posting Veteran

89 Posts

Posted - 2011-01-27 : 10:36:06
I have a varchar column of format mmddyyyy that I want to copy into a column of type Datetime. How can I do that?

Thanks,
Dave

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-01-27 : 10:42:06
convert(datetime,stuff(stuff(@chr,5,0,'/'),3,0,'/'),101)

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

DaveBF
Yak Posting Veteran

89 Posts

Posted - 2011-01-27 : 11:26:32
Thank you very much, nigelrivett. This worked for me.

I'm sorry to hear your beer is not cold. Perhaps you could come to the U.S. for a nice cold Bud.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-01-28 : 01:36:35
This is will work regardless of date settings

select CAST(RIGHT(@chr,4)+LEFT(@chr,4) as DATETIME)


Madhivanan

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

- Advertisement -