Date conversion and culture: Difference between DATE and DATETIME

后端 未结 2 722
离开以前
离开以前 2020-11-27 07:58

I\'ve written a lot of answers about date or datetime conversions from strings. Living in a german speaking country, I\'m used to deal with non

2条回答
  •  萌比男神i
    2020-11-27 08:18

    (Started out as a comment, but...) To add some more data to marc_s's answer, datetime will also fail with yyyy-mm-dd HH:MM:ss - So

    SET LANGUAGE GERMAN;
    DECLARE @dateTimeFailes DATETIME ='2017-01-13 00:00:00';
    

    Will result with the same error. However, Once you replace the space between the Date and Time parts with a T, SQL Server will suddenly understand the format -

    SET LANGUAGE GERMAN;
    DECLARE @dateTime DATETIME ='2017-01-13T00:00:00';
    

    Will work as expected.

    Here is a quick list of acceptable and unacceptable formats:

    SET LANGUAGE GERMAN;
    -- Correct 
    DECLARE @date DATE ='2017-01-13';
    DECLARE @dateTime DATETIME ='2017-01-13T00:00:00'; 
    DECLARE @dateTimeDateOnly DATETIME ='20170113';
    
    -- Incorrect
    DECLARE @WrongFormatDateTime DATETIME ='2017-01-13 00:00:00'; 
    DECLARE @WrongFormatDate DATETIME ='2017-01-13';
    

提交回复
热议问题