sponsor

Wednesday, December 31, 2008

calculate age with mysql

you can use mysql to determine how many years old of your employee is. This is just compute the difference in the year part of the current date and the birth date, then subtract one if the current date occurs earlier in the calendar year than the birth date. The following query shows, for each employee, name, birth date, the current date, and the age in the years. In this example I use table employee which has fields : employee_id, employee_name, birthdate

SELECT employee_id, employee_name, birthdate, CURDATE(),
(YEAR(CURDATE())-YEAR(birthdate))
- (RIGHT(CURDATE(),5)<RIGHT(birthdate,5))
AS age
FROM employee

No comments: