Date issue in Query

  • This is not correct.

    Using BETWEEN in this scenario will give you everything from midnight 2006-05-01 up to and including midnight 2006-05-02.

    /Kenneth

  • The following is wrong because it leaves out one entire second of the day...

    BETWEEN '2006-05-01 00:00:00.000' AND '2006-05-01 23:59:59.000'

    ...depending on the source of the data, there could be thousands of records in that one second.

    Kenneth is absolutely correct in implying that the other BETWEEN scenarios are wrong because if you have any date with "no times" from the next day, they will erroneously be included.

    The proper way to do this and still have indexes kick in when available is...

    WHERE somedatecolumn >= '2006-05-01'

      AND somedatecolumn <  '2006-05-02'

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 16 through 16 (of 16 total)

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