• I forget who I got this one from but I've never found anything simpler for correctly calculating age in years. Using Lynn's good test harness...

    CREATE TABLE #T(Id INT,date1 DATETIME,date2 DATETIME)

    INSERT INTO #T

    SELECT 1,'2/1/2007','1/31/2010' UNION ALL

    SELECT 2,'2/1/2007','2/1/2010' UNION ALL

    SELECT 3, '2/1/2007','1/15/2010' UNION ALL

    SELECT 4, '2/1/2007','2/2/2010'

    SELECT

    Id,

    date1 AS 'Start',

    date2 AS 'End',

    YEAR(date2 - DATEPART(dy, date1) + 1) - YEAR(date1)

    FROM #T

    DROP TABLE #T

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