DateTime data types

  • Comments posted to this topic are about the item DateTime data types

  • Thanks for start my day with an easy question.

    Still i found result as "Correct answers: 100%"

    Nice question, keep it up my friend !!!

    🙂

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Really good question 🙂

    +1

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Got it right by elimination. There is no way a NULL value would be converted to an actual date (as far as I know, at least), so that eliminates answer B. Date types can't return an empty string (that would have to be char, varchar, etc.), so that eliminates answer A.

    However, I never memorized the date that is equivalent to 0 and I didn't know if datetime, datetime2 and date had different conversions from integers, so if there had been an answer such as NULL, 1900-01-01 00:00:000, 1753-01-01 00:00:000, 1900-01-01, I might had gotten it wrong. Guess I learned that much from this question, then.

    Thanks, Sasidhar Pulivarthi! 😀

    Edit: changed "don't know if datetime (...) have different conversions from integers" to "didn't know", which made me realized I actually learned something new here. :w00t:

  • If default values are returned as in your explaination why are not all the values except null returned in this format, "1900-01-01 00:00:00.0000000". How did they get switched to "Jan 1 1900 12:00AM"?

  • Thanks

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Primo Dang (4/11/2013)


    Got it right by elimination

    Thanks, Sasidhar Pulivarthi! 😀

    +1

    Thanks for the question ..

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • jlennartz (4/11/2013)


    If default values are returned as in your explaination why are not all the values except null returned in this format, "1900-01-01 00:00:00.0000000". How did they get switched to "Jan 1 1900 12:00AM"?

    the default conversion style for cast and convert is 0 for datetime and 121 for datetime2 ; that's how !!

    ref: http://msdn.microsoft.com/en-gb/library/ms187928.aspx

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • Primo Dang (4/11/2013)


    Got it right by elimination. There is no way a NULL value would be converted to an actual date (as far as I know, at least), so that eliminates answer B. Date types can't return an empty string (that would have to be char, varchar, etc.), so that eliminates answer A.

    However, I never memorized the date that is equivalent to 0 and I didn't know if datetime, datetime2 and date had different conversions from integers, so if there had been an answer such as NULL, 1900-01-01 00:00:000, 1753-01-01 00:00:000, 1900-01-01, I might had gotten it wrong. Guess I learned that much from this question, then.

    Thanks, Sasidhar Pulivarthi! 😀

    Edit: changed "don't know if datetime (...) have different conversions from integers" to "didn't know", which made me realized I actually learned something new here. :w00t:

    i too memorize if we use datatime2 datatype that gives the datetime value as '1900-01-01 00:00:00.0000000 '. That is the reason to clicked the third answer... it's correct.....

    thanks for this question.. nice one....

    Manik
    You cannot get to the top by sitting on your bottom.

  • demonfox (4/11/2013)


    jlennartz (4/11/2013)


    If default values are returned as in your explaination why are not all the values except null returned in this format, "1900-01-01 00:00:00.0000000". How did they get switched to "Jan 1 1900 12:00AM"?

    the default conversion style for cast and convert is 0 for datetime and 121 for datetime2 ; that's how !!

    ref: http://msdn.microsoft.com/en-gb/library/ms187928.aspx

    Thank You

  • demonfox (4/11/2013)


    Primo Dang (4/11/2013)


    Got it right by elimination

    Thanks, Sasidhar Pulivarthi! 😀

    +1

    Thanks for the question ..

    +1

    Thanks

  • What I find annoying is that by assigning a value of '' to the Datetime2 variable is that it has gone for '1900........'

    My preference would be to set the variable to its lowest possible value which is '0001-01-01..........'

    I guess that Microsoft has gone for consistency ?

    David

  • +1..

    Thanks for the question..

  • Thanks for the question.

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

  • David Conn (4/12/2013)


    What I find annoying is that by assigning a value of '' to the Datetime2 variable is that it has gone for '1900........'

    My preference would be to set the variable to its lowest possible value which is '0001-01-01..........'

    I guess that Microsoft has gone for consistency ?

    David

    The default value is equivalent to zero, at least for the older datatypes, DateTime and SmallDateTime, and zero is defined to be 1900-01-01. Older dates are therefore "negative numbers". I tried to modify the question to set the 4 variables = 0, but learned that this is not allowed for the newer DateTime2 and Date datatypes (even with an explicit conversion) (at least, in SQL 2008).

    Msg 529, Level 16, State 2, Line 6

    Explicit conversion from data type numeric to date is not allowed.

    So, to demonstrate the zeros, I ended up with:

    Declare @smldatetime SmallDateTime

    , @dt DateTime

    , @dt2 DateTime2

    , @d Date

    Select

    @d=''

    , @smldatetime=0

    , @dt=0

    , @dt2 =''

    Select

    CAST(@d as varchar)

    , CAST(@smldatetime as VARCHAR)

    , CAST(@dt as VARCHAR)

    , CONVERT(varchar,@dt2 )

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

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