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:
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.
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 .
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).
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!)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With