question

msharp avatar image
msharp asked

Languages, date formats, CONVERT() and CAST()

Working in a culture which uses dd/mm/yyyy date formats can lead to some peculiar problems when using US-centric software. Past experience with this problem lead me to consistently express date values as yyyy-mm-dd for SQLServer. I was under the impression that this was a foolproof, culture-neutral technique and until now I have not had any problem with it. Today I encountered a peculiar problem with casting or converting date-strings when different language settings are applied. This is OK. SET LANGUAGE 'us_english' SELECT CAST('2010-7-13' AS DATETIME) Whereas these two fail with the message: *The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.* SET LANGUAGE 'British' SELECT CAST('2010-7-13' AS DATETIME) SET LANGUAGE 'Italian' SELECT CAST('2010-7-13' AS DATETIME) Even stranger is that this succeeds SET LANGUAGE 'British' SELECT CONVERT(DATETIME,'2010-7-13',101) But this fails SET LANGUAGE 'British' SELECT CONVERT(DATETIME,'2010-7-13') Can anyone explain what is going on here?
sql-servercast-convertdateformatlanguage
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Daniel Ross avatar image
Daniel Ross answered
If you took out the Hyphens in the date format it would work. See this URL for a list of Date formats and multi language compatability http://msdn.microsoft.com/en-us/library/ms180878.aspx. Also see here for multi language TSQL statements. http://msdn.microsoft.com/en-us/library/ms191307.aspx. It basically says that you need to specify the style parameters with a convert statement
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks Daniel. Very interesting. Once again my firmly-held assumptions are proved invalid.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.