0

Here is a simple script to calculate date of birth from getdate or any set date. I've seen many answers to this calculation but all seem to have issues like rounding and such. Do you see any issues with script below for age calculation?

DECLARE @DoB AS DATE = '1968-10-24'
DECLARE @cDate AS DATE = CAST('2000-10-23' AS DATE)
SELECT 

--Get Year difference

    DATEDIFF(YEAR,@DoB,@cDate) -

    --Cases where year difference will be augmented
    CASE 
        --If Date of Birth greater than date passed return 0
        WHEN YEAR(@DoB) - YEAR(@cDate) >= 0 THEN DATEDIFF(YEAR,@DoB,@cDate)

        --If date of birth month less than date passed subtract one year
        WHEN MONTH(@DoB) - MONTH(@cDate) > 0 THEN 1 

        --If date of birth day less than date passed subtract one year
        WHEN MONTH(@DoB) - MONTH(@cDate) = 0 AND DAY(@DoB) - DAY(@cDate) > 0 THEN 1 

        --All cases passed subtract zero
        ELSE 0
    END

0 Answers0