x
login about faq Site discussion (meta-askssc)

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 '10 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 '10 at 08:38 PM

Daniel Ross gravatar image

Daniel Ross
2.9k 6 10 12

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

Jul 12 '10 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x29
x17
x7
x6
x4

asked: Jul 12 '10 at 07:44 PM

Seen: 1479 times

Last Updated: Jul 12 '10 at 07:44 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.