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 2005 Forums
 Transact-SQL (2005)
 Coverting VARCHAR date to datetime

Author  Topic 

allisd`17
Starting Member

1 Post

Posted - 2009-05-05 : 15:12:19
I need help converting the below varchar date to date time so i can compare it to current date.

10/29/2008 7:04:46 AM
or it can be
5/5/2009 10:05:47 AM

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-05-05 : 15:16:39
declare @var varchar(50)

set @var = '5/5/2009 10:05:47 AM'

select convert(datetime,@var)


Jim
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 15:17:04
Use CAST or CONVERT functions.

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

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-05-05 : 15:28:47
quote:
Originally posted by jimf

declare @var varchar(50)

set @var = '5/5/2009 10:05:47 AM'

select convert(datetime,@var)


Jim



i think this will only work as long as you have db language set having dateformat as mm/dd/yyyy.

rather use this,
select convert(datetime,'10/29/2008 7:04:46 AM',101)
select convert(datetime,'5/5/2009 10:05:47 AM',101)
Go to Top of Page
   

- Advertisement -