DateTime Precision

  • Comments posted to this topic are about the item DateTime Precision

  • As Sherlock Holmes said to Dr. Watson ................ Very interesting

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Good observation. Thanks.

    M&M

  • Don't you hate it when the explanation says, "Despite what BOL says..."

    It should either be mentioned in BOL or do what the BOL says.

  • cengland0 (11/27/2011)


    Don't you hate it when the explanation says, "Despite what BOL says..."

    It should either be mentioned in BOL or do what the BOL says.

    Heck no one is perfect, and if memory serves me correctly those who write the explanations for BOL are NOT the developers or actual personnel writing the code ...

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • The question is not really about datetime precision, but string comparison.

    If the comparison value had been declared as datetime, then the other values would be implicitly converted to datetime as well and the statement would return Yes.

    declare @d as datetime

    set @d = '2011-07-31 00:00:00.000'

    IF @d BETWEEN '2011-07-01' and '2011-07-31'

    PRINT 'Yes'

    ELSE

    PRINT 'No'

    The question as it stands could just as well have been this

    if ('abcd' between 'ab' and 'abc')

    print 'yes'

    else

    print 'no'

  • Nice tricky question with stupid 'explanation'.

    Of course, the string (not datetime) '2011-07-31 00:00:00.000' is greater than the other string '2011-07-31', and thus the result of the batch is 'No'.

  • Glad I checked out the comments after getting this one wrong. I hate learning things that aren't true. 🙂

    -- Stephen Cook

  • Silly trick question...

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

  • Jostein Saethern (11/27/2011)


    The question is not really about datetime precision, but string comparison.

    If the comparison value had been declared as datetime, then the other values would be implicitly converted to datetime as well and the statement would return Yes.

    declare @d as datetime

    set @d = '2011-07-31 00:00:00.000'

    IF @d BETWEEN '2011-07-01' and '2011-07-31'

    PRINT 'Yes'

    ELSE

    PRINT 'No'

    The question as it stands could just as well have been this

    if ('abcd' between 'ab' and 'abc')

    print 'yes'

    else

    print 'no'

    You are right, I'ts very obvious when you think about it, but when you see something that looks like a date, you assume that SQL server see it as a date. BUT it would be dangerous if SQL server would interpret the datatypes based on the values, and this is just another reason to dislike the implicit conversions. (I know, there is no implicit conversion in the question, but we have learned to live with them so long that we assume it takes place all the time).

    I really would like a database setting, a trace flag or something that warn you or prevent you from using implicit conversion and force you to use explicit conversion. Because if you do, you know that you are doing something wrong (from a performance perspective).

    The explanation is not correct and should be changed.

    /Håkan Winther
    MCITP:Database Developer 2008
    MCTS: SQL Server 2008, Implementation and Maintenance
    MCSE: Data Platform

  • It is a string comparation.

    Dates comparation might be:

    IF (CAST( '2011-07-31 00:00:00.000' AS DATETIME) BETWEEN CAST( '2011-07-01' AS DATETIME) and CAST( '2011-07-31' AS DATETIME) )

    PRINT 'Yes'

    ELSE

    PRINT 'No'

    Regards,

    Iulian

  • Hi jkelly - tricky question!

  • Thanks for the question and also to the posts gave me something to think about on a cold Monday morning.

  • As several people have pointed out, this "explanation" is bogus.

    SQL Server is comparing these values as strings - this has nothing to do with datetime precision. Replace the '2011-07-31 00:00:00.000' with '2011-07-31 REDHERRING' and you get the same result.

  • I overlooked that it is a STRING comparison and got the answer wrong.:-) (though writer had intention to show datetime comparison)

    I updated the code as follows and executed:

    DECLARE @DATE DATETIME

    SET @DATE = '2011-07-31 00:00:00.001'

    IF (@DATE BETWEEN '2011-07-01' and '2011-07-31')

    PRINT 'Yes'

    ELSE

    PRINT 'No'

    The output came 'Yes'. However when I changed the @date value to '2011-07-31 00:00:00.002' the result was 'No'. As per my knowledge the @date value should be compared to the end date '2011-07-31 00:00:00.000'. How '2011-07-31 00:00:00.001' be less than or equal to '2011-07-31 00:00:00.000'?:unsure:

    I executed this on SQL Sever 2008 EE Ver. 10.0.4064.0

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

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