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)
 Convert string to date

Author  Topic 

rookie_sql
Constraint Violating Yak Guru

443 Posts

Posted - 2009-01-07 : 08:02:51
Hi am ina bit of a hurry and can't figure out how to convert the date which is a varchar datatime to a datetime datatype.

Example of data

01/01/08

I tried to use convert(datetime,col,103) as Date_dte but it did not work..

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-07 : 08:04:37
convert(datetime,col) should be enough
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 08:05:21
What is the error you got?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-07 : 08:06:30
It depends on the input format but something like this would work:

DECLARE @date varchar(30)
SET @date = '01/01/08 '
SELECT CAST(@date AS DATETIME)

- Lumbago
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 08:10:16
Unless data '17/12/08' is stored. SQL Server may not be able to determine which dateformat is used.

CONVERT(DATETIME, Col1, <fmt> ) should be used to ensure that same conversion is made for all records.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-07 : 08:12:50
quote:
Originally posted by Peso

Unless data '17/12/08' is stored. SQL Server may not be able to determine which dateformat is used.

CONVERT(DATETIME, Col1, <fmt> ) should be used to ensure that same conversion is made for all records.



E 12°55'05.63"
N 56°04'39.26"




Don't we use <fmt> only when converting to varchar ?
select convert(datetime,'01/01/08')
works fine.
select convert(datetime,'01/01/08',103)
gives error.
Syntax error converting datetime from character string.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 08:17:47
Of course!
You are implying that century is used too, by writing 103, but there is no century in the data.

select convert(datetime,'01/01/08', 3)
select convert(datetime,'01/01/2008', 103)

works both ok.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-07 : 08:21:08
followed, thanks.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 08:31:05
Based on that, rookie_sql should use the value 3 for format in his original post.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

rookie_sql
Constraint Violating Yak Guru

443 Posts

Posted - 2009-01-07 : 09:22:12
Thanks everyone
select convert(datetime,'01/01/08',103) work for me now. I also though it might be a space issues so i used the replace function to clean up any spaces.
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-07 : 09:27:37
quote:
Originally posted by rookie_sql

Thanks everyone
select convert(datetime,'01/01/08',103) work for me now. I also though it might be a space issues so i used the replace function to clean up any spaces.



how convert(datetime,'01/01/08',103) this works for u ??

u can use only

select convert(datetime,'01/01/08', 3)
select convert(datetime,'01/01/2008', 103)

as peso suggested
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 09:37:07
Maybe a typo? Or not using Microsoft SQL Server?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -