x

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?

more ▼

asked Jul 12, 2010 at 07:44 PM in Default

msharp gravatar image

msharp
13 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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
more ▼

answered Jul 12, 2010 at 08:38 PM

Daniel Ross gravatar image

Daniel Ross
2.9k 10 13 14

Thanks Daniel. Very interesting. Once again my firmly-held assumptions are proved invalid.
Jul 12, 2010 at 10:53 PM msharp
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x344
x29
x7
x6

asked: Jul 12, 2010 at 07:44 PM

Seen: 2747 times

Last Updated: Jul 12, 2010 at 07:44 PM