Calculate Age in SQL :
DECLARE @birthDate date= '1980-07-02', @today date=GETDATE()
DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = @birthDate
SELECT @tmpdate = @date
SELECT @years = DATEDIFF(yy, @tmpdate, @today) - CASE WHEN (MONTH(@date) > MONTH(@today)) OR (MONTH(@date) = MONTH(@today) AND DAY(@date) > DAY(@today)) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, @today) - CASE WHEN DAY(@date) > DAY(@today) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, @today)
SELECT convert(varchar,@years)+'Y '+ convert(varchar,@months)+'M '+ convert(varchar,@days)+'D' AS Age
0 Comments