• Koen Verbeeck (2/26/2014)


    I believe the date functions are more precise in that aspect.

    I certainly agree that the date functions make life a whole lot easier in certain areas but I don't agree that they are any more accurate when it comes to durations and spans of time. Here's a simple example of displaying the difference between two dates and times in the classic HHHH:MM:SS:mmm format and the classic decimal hours format. If there's a simpler or more performant method to do the same things using only date fuctions, I'd love to see them. To be sure, there is no sarcasm or irony in that statement. I'd really like to know.

    DECLARE @StartDate DATETIME

    ,@EndDate DATETIME

    ;

    SELECT @StartDate = '31 Dec 2014 23:59:59.997'

    ,@EndDate = '01 Jan 2016 00:06:59.000'

    ;

    --===== Subtraction method for display

    SELECT CONVERT(VARCHAR(10),DATEDIFF(hh,0,@EndDate-@StartDate))

    + RIGHT(CONVERT(CHAR(12),@EndDate-@StartDate,114),10)

    ;

    --===== Subtraction method for decimal hours

    SELECT CONVERT(DECIMAL(11,1),(CONVERT(FLOAT,@EndDate-@StartDate)*24.0))

    ;

    {EDIT} Correct spelling on the word "function".

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