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

折月煮酒 提交于 2019-12-10 12:40:57

问题


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.


回答1:


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.




回答2:


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.




回答3:


Hard to answer a question starting with "Why" :-)

This may not answer your question, but for dates there is one string format which will work across ALL locales: 'YYYYMMDD'

Try:

SET LANGUAGE English
SELECT CAST('20130815' AS DATETIME)

SET LANGUAGE German
SELECT CAST('20130815' AS DATETIME)

SET LANGUAGE Japanese
SELECT CAST('20130815' AS DATETIME)

This will give the expected result.



来源:https://stackoverflow.com/questions/21302732/sql-server-why-are-dates-in-iso-8601-format-language-dependent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!