gives a fabulous formula:
[tt]select ( YEAR(CURRENT_DATE) - YEAR(birth) )
- ( RIGHT(CURRENT_DATE,5) < RIGHT(birth,5) )
as age[/tt]
what's happening here may be hard to see at first
if today's MMDD is less than the birthdate MMDD, then the "less than" evaluates TRUE, which mysql interprets as a 1, which is then subtracted from the difference in years
e.g. if your birthdate is november 26, 1982, then 2002-1982 equals 20, but since 1117 (today) is less than 1126 (birth), you have to subtract 1... so you're 19
the mysql RIGHT function is operating on the YYYYMMDD format of the date as a string
in ansi sql, you could do this:
[tt]select ( YEAR(CURRENT_DATE) - YEAR(birth) )
- ( CASE
WHEN month(CURRENT_DATE) < month(birth)
THEN 1
WHEN month(CURRENT_DATE) = month(birth)
AND day(CURRENT_DATE) < day(birth)
THEN 1
ELSE 0
END ) as age[/tt]
rudy