Pages

Calculate Age from Database in MySQL

This function can be used to calculate age.
DELIMITER $$

DROP FUNCTION IF EXISTS Age $$

CREATE FUNCTION Age( dob DATE )
RETURNS CHAR(20)
BEGIN
DECLARE years INT default 0;
DECLARE months INT default 0;
DECLARE days INT default 0;
DECLARE age DATE;

-- Check that the dob we're given is useful
IF dob is null or dob = 0 or dob = '0000-00-00' THEN
 RETURN dob;
END IF;
SELECT date_add('0001-01-01', interval datediff(current_date(),dob) day ) INTO age;
SELECT YEAR(age) -1 INTO years;
SELECT MONTH(age)-1 INTO months;
SELECT DAY(age) -1 INTO days;

IF years THEN
 RETURN concat(years,'y ',months,'m');
ELSEIF months THEN
 RETURN concat(months,'m ',days,'d');
ELSE
 RETURN concat(days,' days');
END IF;
END $$
DELIMITER ;

Use

SELECT Age(DOB) FROM table; 
Source : http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html