问题描述
我需要一些帮助,了解SQL Server中的日期格式处理。
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
但是,这将导致转换错误,因为SQL Server将8视为当天的15,而是将月份解释为15
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.
我知道我可以使用语言无关(稍微改编的ISO-8601)格式 YYYYMMDD
(不用破折号),它可以使用任何语言。
I know that I can use the language-independent (slightly adapted ISO-8601) format YYYYMMDD
(without dashes), and it will work in any language.
我不明白为什么 YYYY-MM-DD
是语言相关的,当SQL Books清楚地说
I don't understand however why YYYY-MM-DD
is language dependent, when SQL Books clearly says
解释取决于字符串文字格式,...和默认语言选项设置的组合...某些字符串文字格式不受这些设置的影响。 .. ISO 8601格式不依赖于这些设置而且是国际标准。
"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."
甚至查看返回的dateformat select * from sys.syslanguages
没有指示 - 日期格式为 dmy
,所以它也不符合ISO-8601格式。
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.
所以,问题是:
- 为什么ISO-8601格式依赖于语言,即使在线书籍说otherwi se?
- 在哪里可以找到SQL Server在解析ISO-8601日期时使用的确切格式?
更新:
进一步了解,它说'要使用ISO 8601格式,您必须以格式指定每个元素。这包括T,冒号(:),+或 - 和句点(。)'(例如2004-05-23T14:25:10)。
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).
上面的表()说,ISO 8601数字不是 DATEFORMAT依赖,但也是不是多语言。我不知道在哪里可以找到关于多语言部分的附加信息 - 例如,每种语言使用的确切格式。
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.
推荐答案
此相关问题可能有助于语言和ISO-8601日期格式。
This related question might help with languages and ISO-8601 date formats.Why is SQL Server misinterpreting this ISO 8601 format date?
请参阅文章,在答案中也链接了SQL Server使用的datetime类型的更多信息。
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.
这篇关于SQL Server:为什么ISO-8601格式的日期依赖于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!