• The problem with DATEDIFF is that it's sensitive to temporal borders. For example, the following two times are only 3 milliseconds apart but the DATEDIFF for hours between the two returns a "1".

    SELECT DATEDIFF(hh,'12:59:59.997', '13:00');

    An easy correction for this "feature" is to simply convert the times to DATETIME, subtract the smaller from the larger, and then do a DATEDIFF between 0 and that previously calculated difference to get the number of whole hours that have passed (without rounding).

    SELECT DATEDIFF(hh,0,CAST('13:00' AS DATETIME)-CAST('12:59:59.997' AS DATETIME))

    --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)