Date Puzzle

  • abmore

    Hall of Fame

    Points: 3628

    Comments posted to this topic are about the item Date Puzzle

  • sharath.chalamgari

    SSCertifiable

    Points: 5680

    Simple but most of the people forget when they use it. Thanks any way

  • Hardy21

    SSCrazy Eights

    Points: 9708

    Question is simple but nice one.

    Thanks

  • Henrik Staun Poulsen

    SSCertifiable

    Points: 6370

    '31-Jan-2010'

    It that the correct (international) way to hard-code dates?

    I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?

  • Hardy21

    SSCrazy Eights

    Points: 9708

    henrik staun poulsen (10/27/2010)


    '31-Jan-2010'

    It that the correct (international) way to hard-code dates?

    I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?

    date is displayed as per your database/server setting.

    you need to convert the string into DATE format if you want only DATE portion (applicable for SQL 2k8 only) or DATETIME format if you want date & time both like:

    SELECT CONVERT(DATE, '10/27/2010')

    if you want to set variable then SELECT @dateValue = CONVERT(DATE, '10/27/2010')

    Thanks

  • Koen Verbeeck

    SSC Guru

    Points: 258955

    This one was too easy, but thanks for the question anyway.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Hugo Kornelis

    SSC Guru

    Points: 64675

    henrik staun poulsen (10/27/2010)


    '31-Jan-2010'

    It that the correct (international) way to hard-code dates?

    I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?

    Hi Henrik,

    A good point. Using '31-Jan-2010' in T-SQL to represent a date is not safe. If the server is set to a language other than English, you might get an error. For instance:

    SET LANGUAGE French;

    SELECT DATEADD(MONTH, 3, '31-Jan-2010');

    Le paramètre de langue est passé à Français.

    -----------------------

    Msg 241, Level 16, State 1, Line 2

    Échec de la conversion d'une valeur datetime à partir d'une chaîne de caractères.

    (The text of these messages roughly translate to "language succesfully set to french" / "error converting character value to datetime")

    For the datetime data type, The only formats that are guaranteed to work correctly in all circumstances are:

    * yyyymmdd - for dates without time portion. No dashes. dots, slashes or other punctuation. The resulting datetime value will have its time portion set to midnight.

    * yyyy-mm-ddThh:mm:ss - for dates with a time portion. Dashes between the date components, colons between the time components, and an uppercase T to seperate date from time.

    * yyyy-mm-ddThh:mm:ss.mmm - as above, but with microseconds added. These will be rounded to the nearest 1/300 of a second.

    For the new date data type, I think (too little hands-on SQL 2008 experience to be sure) that you can choose between these two formats:

    * yyyymmdd - same as for datetime values, use when compatibility among date/time types is required

    * yyyy-mm-dd - as presribed by ISO standards, but unfortunately not guaranteed for datetime; the new date data type finally allowed the SQL Server team to fix this (though unfortunately not for the old types - for backwards compatibility reasons, I guess)


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • archie flockhart

    SSCrazy

    Points: 2339

    I might have had to think twice if the possible answers had been April 30th, April 31st and May 1st.

  • Hugo Kornelis

    SSC Guru

    Points: 64675

    Hardy21 (10/27/2010)


    henrik staun poulsen (10/27/2010)


    '31-Jan-2010'

    It that the correct (international) way to hard-code dates?

    I think I would have used '2010-01-31' or is it '20100131' to ensure that it will work around the globe?

    date is displayed as per your database/server setting.

    you need to convert the string into DATE format if you want only DATE portion (applicable for SQL 2k8 only) or DATETIME format if you want date & time both like:

    SELECT CONVERT(DATE, '10/27/2010')

    if you want to set variable then SELECT @dateValue = CONVERT(DATE, '10/27/2010')

    As far as I know (but again, my SQL 2008 experience is limited), this will fail if you have a server language (or a SET LANGUAGE) that does not use the mm/dd/yyyy format. (Which means, almost everywhere except the US).

    For instance (substituting datetime for date, for I run SQL 2005):

    SET LANGUAGE British;

    SELECT CONVERT(DATETIME, '10/27/2010')

    Changed language setting to British.

    -----------------------

    Msg 242, Level 16, State 3, Line 2

    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • hodgy

    SSCertifiable

    Points: 5735

    archie flockhart (10/27/2010)


    I might have had to think twice if the possible answers had been April 30th, April 31st and May 1st.

    My thoughts exactly

    Life: it twists and turns like a twisty turny thing

  • Toreador

    SSChampion

    Points: 11248

    Hugo Kornelis (10/27/2010)


    For the datetime data type, The only formats that are guaranteed to work correctly in all circumstances are:

    * yyyymmdd - for dates without time portion. No dashes. dots, slashes or other punctuation. The resulting datetime value will have its time portion set to midnight.

    We use yyyy-mm-dd which has always worked so far.

    Should I be worried?!

  • DugyC

    Hall of Fame

    Points: 3804

    I'm absolutely gob smacked somebody answered 29Apr, or even worse 31Mar. :ermm:

    I'll give them the benefit of the doubt though and presume they suspected a trick question. 😀

    Agree with the previous poster that 30Apr, 31Apr, 1May would have been more teasing options.

    Good question though, I like the easy ones... coz I can answer those 😛

    _____________________________________________________________________
    [font="Comic Sans MS"]"The difficult tasks we do immediately, the impossible takes a little longer"[/font]

  • Hugo Kornelis

    SSC Guru

    Points: 64675

    Toreador (10/27/2010)


    Hugo Kornelis (10/27/2010)


    For the datetime data type, The only formats that are guaranteed to work correctly in all circumstances are:

    * yyyymmdd - for dates without time portion. No dashes. dots, slashes or other punctuation. The resulting datetime value will have its time portion set to midnight.

    We use yyyy-mm-dd which has always worked so far.

    Should I be worried?!

    set language German;

    select cast('2010-10-27' as datetime);

    select cast('20101027' as datetime);

    Die Spracheneinstellung wurde auf Deutsch geändert.

    -----------------------

    Msg 242, Level 16, State 3, Line 2

    Bei der Konvertierung eines char-Datentyps in einen datetime-Datentyp liegt der datetime-Wert außerhalb des gültigen Bereichs.

    -----------------------

    2010-10-27 00:00:00.000

    German was the first of the languages I tested that returned an error when using yyyy-mm-dd. I did not try the remaining languages. You can easily do that for yourself.

    (For a list of possible languages, run "select * from sys.syslanguages;", and use either the "name" or the "alias" column as argument for SET LANGUAGE).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • pravin wagh

    SSC Veteran

    Points: 223

    easy question

  • SanDroid

    SSChampion

    Points: 10068

    I give this question Five Stars!

    It was code correct, the question was clear and had no typo's, and the author clearly understood the concept his question was testing.

    This Question of the Day has been plagued lately with poorly worded and misspelled code examples and questions. That along with the fact they they were complex questions with complex SQL Server features and statements made them horible.

Viewing 15 posts - 1 through 15 (of 48 total)

You must be logged in to reply to this topic. Login to reply