Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Why are dates in ISO-8601 format language dependent?

I need some help understanding date format handling in SQL Server.

If you try the following, it will return a correct result:

SET LANGUAGE English
SELECT CAST('2013-08-15' AS DATETIME)

-- 2013-08-15 00:00:00.000

This, however, will result in a conversion error because apparently SQL Server interprets '8' as the day and '15' as the month:

SET LANGUAGE German
SELECT CAST('2013-08-15' AS DATETIME)

-- Conversion failed when converting date and/or time from character string.

I know that I can use the language-independent (slightly adapted ISO-8601) format YYYYMMDD (without dashes), and it will work in any language.

I don't understand however why YYYY-MM-DD is language dependent, when SQL Books clearly says

"The interpretation depends on the combination of string literal format, ... and default language option settings. ... Some string literal formats are not affected by these settings. ... The ISO 8601 format does not depend on these settings and is an international standard."

http://technet.microsoft.com/en-us/library/ms180878%28v=sql.105%29.aspx

Even looking at the dateformat returned by select * from sys.syslanguages gives no indication - the date format is dmy, so it doesn't match the ISO-8601 format either.

So, the questions are:

  • Why is the ISO-8601 format language-dependent, even though Books Online says otherwise?
  • Where can I find the exact format SQL Server uses when parsing ISO-8601 dates?

UPDATE:

Reading further down on http://technet.microsoft.com/en-us/library/ms180878%28v=sql.105%29.aspx#ISO8601Format, it says 'To use the ISO 8601 format, you must specify each element in the format. This includes the T, the colons (:), the + or - , and the periods (.)' (e.g. 2004-05-23T14:25:10).

The table right above (http://technet.microsoft.com/en-us/library/ms180878%28v=sql.105%29.aspx#StringLiteralDateandTimeFormats) says that the ISO 8601 Numeric is not DATEFORMAT dependent, but it also is not Multilanguage. I'm not sure where to find additional information about the Multilanguage part though - e.g., the exact format used in each language.

like image 541
matk Avatar asked Jan 23 '14 08:01

matk


People also ask

Is ISO 8601 always UTC?

Date.prototype.toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What is the date format according to ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

Why is ISO 8601 better?

We've chosen ISO 8601 because it's a worldwide standard all modern programming languages support. ISO 8601 provides high precision, is timezone aware and is easy for humans to understand. It's also used by NASA, (which will be useful if GDS goes into space!)


2 Answers

This related question might help with languages and ISO-8601 date formats. Why is SQL Server misinterpreting this ISO 8601 format date?

See the article The ultimate guide to the datetime datatypes which was also linked in the answer for more information on the datetime types used by SQL Server.

like image 69
Ilessa Avatar answered Sep 21 '22 10:09

Ilessa


My guess would be to maintain backwards compatibility. The new datatypes in SQL Server 2008 datetime2 and date is not dependent on SET LANGUAGE or SET DATEFORMAT. Here is a connect item that suggests to change the behaviour for datetime as well.

like image 23
Mikael Eriksson Avatar answered Sep 17 '22 10:09

Mikael Eriksson