Home Forums Programming General Help calculating the age from the date of birth in table RE: Help calculating the age from the date of birth in table

  • It's been a while since I'd use or suggest a scalar function as a solution to a problem.

    Instead, this should do it.

    SELECT dob,

    CASE WHEN DATEADD(YYYY, DATEDIFF(YYYY, dob, GETDATE()), dob) > GETDATE()

    THEN DATEDIFF(YYYY, dob, GETDATE()) - 1

    ELSE DATEDIFF(YYYY, dob, GETDATE())

    END

    FROM (VALUES(CAST('19851115' as date)), ('19850131'), ('19850302'), ('19840302'))x(dob) --Sample dates

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2