| Author |
Topic |
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2007-09-07 : 05:27:03
|
| Hello,I'm in Query Analyser at the moment but am having problem with one of my fields.My code is below :SELECTUniqueID,EmployeeNumber,convert(datetime,[Date], 102) as OTDate,DetailsFROMdbo.Staging_OTHowever, I'm getting an error saying :Server: Msg 241, Level 16, State 1, Line 4Syntax error converting datetime from character string.Can anyone tell me why this is, and how I can get round it please ?Thanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 05:34:05
|
what is the format of your date in the table ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2007-09-07 : 05:41:03
|
| It's a varchar(50) in the table and an example of the data in there is : 4/3/2003However the reason I'm creating this SQL is to take it into my actual table (i.e. not the Staging Table) where the format is smalldatetime. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 05:46:00
|
4/3/2003 is MM/DD/YYYY or DD/MM/YYYY ?use convert(datetime,[Date], 101) as OTDate for MM/DD/YYYYor convert(datetime,[Date], 103) as OTDate for DD/MM/YYYY102 is for yy.mm.dd KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-07 : 05:47:53
|
| Find the duff dates:-- SET DATEFORMAT MDY -- Set date format here, if neededSELECT TOP 100 [Problem date] = [Date], *FROM dbo.Staging_OTWHERE IsDate([Date]) = 0Kristen |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-07 : 05:49:19
|
| Note that if you useSET DATEFORMAT dmyyou don;t need to worry about using a CONVERT(datetime, string, 999) style fixed-format conversion, the implicit conversion will use the current SET DATEFORMATKristen |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 05:49:53
|
1.5 X  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2007-09-07 : 06:13:46
|
| Hello,Firstly, thanks for agreeing to help me - I appreciate it.Secondly, I tried convert(datetime,[Date], 103) as OTDate as advised by Khtan, but got the following error :==============(6 row(s) affected)Server: Msg 242, Level 16, State 3, Line 4The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.==============I try to find the duff dates as advised by Kristen, but I'm not sure what it's meant to show me.Basically it shows me a "problem date" column with the value "4/22/2003" and a "date" column which has exactly the same value in it.I'm a bit confused. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-07 : 06:39:26
|
| That means that "4/22/2003" is not recognised as a valid date, so presumably your dates are in Month, Day, Year sequence, so try using DATEFORMAT - i.e.SET DATEFORMAT MDYSELECT TOP 100 [Problem date] = [Date], *FROM dbo.Staging_OTWHERE IsDate([Date]) = 0Kristen |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-07 : 06:42:59
|
That means your date format is MM/DD/YYYYY you should use convert with style 101 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2007-09-07 : 06:46:36
|
| Thank you both so much - I appreciate all of your help in resolving my problem. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-07 : 06:52:09
|
"That means your date format is MM/DD/YYYYY you should use convert with style 101"Nah, you're being optimistic. It means that THAT DATE is M/D/Y ... all the rest may be D/M/Y |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-07 : 08:46:11
|
quote: Originally posted by Kristen That means that "4/22/2003" is not recognised as a valid date, so presumably your dates are in Month, Day, Year sequence, so try using DATEFORMAT - i.e.SET DATEFORMAT MDYSELECT TOP 100 [Problem date] = [Date], *FROM dbo.Staging_OTWHERE IsDate([Date]) = 0Kristen
Note that like ISNUMERIC(), ISDATE() is not always reliableSELECT ISDATE(2000),ISDATE('2000')MadhivananFailing to plan is Planning to fail |
 |
|
|
|