Hi JJx5,
One problem I found with this age calculation - The calculation errors out when the date you are calculating from is 2/29/yyyy. Because you are subtracting years from this date it can result in an invalid date error (eg - 2/29/2011).
The best age calculation I have found is:
(EXTRACT(YEAR FROM ((CAST(CURRENT_DATE AS DATE)-CAST(birth_date AS DATE)) YEAR(4) TO MONTH))) -
CASE WHEN EXTRACT(MONTH FROM CURRENT_DATE) = EXTRACT(MONTH FROM BIRTH_DATE) AND EXTRACT(DAY FROM BIRTH_DATE) > EXTRACT(DAY FROM CURRENT_DATE) THEN 1 ELSE 0 END AS age
I have yet to find any case where this does not work, but I am sure there is at least one case out there somewhere.
-Kevin
Hi JJx5,
One problem I found with this age calculation - The calculation errors out when the date you are calculating from is 2/29/yyyy. Because you are subtracting years from this date it can result in an invalid date error (eg - 2/29/2011).
The best age calculation I have found is:
(EXTRACT(YEAR FROM ((CAST(CURRENT_DATE AS DATE)-CAST(birth_date AS DATE)) YEAR(4) TO MONTH))) -
CASE WHEN EXTRACT(MONTH FROM CURRENT_DATE) = EXTRACT(MONTH FROM BIRTH_DATE) AND EXTRACT(DAY FROM BIRTH_DATE) > EXTRACT(DAY FROM CURRENT_DATE) THEN 1 ELSE 0 END AS age
I have yet to find any case where this does not work, but I am sure there is at least one case out there somewhere.
-Kevin