The ISO-8601 for DATETIME
(the older type) is somehow “broken” or “adapted” (depending on whether you look at it as a bug or a feature) – you need to use YYYYMMDD
(without any dashes) to make it work irrespective of the language settings.
For DATE
or the DATETIME2(n)
datatypes, this has been fixed and the “proper” ISO-8601 format YYYY-MM-DD
will always be interpreted correctly.
-- OK because of "adapted" ISO-8601
SET LANGUAGE GERMAN;
DECLARE @dt DATETIME='20170113';
SELECT @dt;
SELECT CAST('20170113' AS DATETIME);
SELECT CONVERT(DATETIME, '20170113');
-- OK because of DATETIME2(n)
SET LANGUAGE GERMAN;
DECLARE @dt2 DATETIME2(0) = '2017-01-13';
SELECT @dt2;
SELECT CAST('2017-01-13' AS DATETIME2(0));
SELECT CONVERT(DATETIME2(0), '2017-01-13');
It’s a quirk of the DATETIME
type (and not the only one….) – just register it, know about it – and move on (meaning: don’t use DATETIME
anymore – use DATE
or DATETIME2(n)
instead – much nicer to work with!) 🙂