Wednesday, December 08, 2004

Selecting an age from a birthday date field in mySQL

I added this to the mySQL manual a while back, but it seems to have been filtered out in more recent versions of the docs. Although I prefer to do my arithmetic in PHP, this query will determine an age from a date field birthDate in mySQL.

SELECT YEAR(NOW()) - YEAR(birthDate) - IF ( MONTH(NOW()) < MONTH(birthDate), 1, 0 ) - IF ( MONTH(NOW()) = MONTH(birthDate) AND DAYOFMONTH(NOW()) < DAYOFMONTH(birthDate), 1, 0) AS age

3 comments:

Bob said...

This is fixed:

SELECT YEAR(NOW()) - YEAR(birthday) - IF ( MONTH(NOW()) < MONTH(birthday), 1, 0 ) - IF ( MONTH(NOW()) = MONTH(birthday) AND DAYOFMONTH(NOW()) < DAYOFMONTH(birthday), 1, 0) AS age, birthday, now() from profile where id = 37;

Dominic Businaro said...

Thanks for this code!

qishaya said...

Milan create week ended last night with donatella christian louboutin london disregard presenting the ending show of the autumn christian louboutin online frost 2003 italian collections.Donatella, the creative chief of christian boots the house her delayed brother founded, delivered a collection that was, christian louboutin uk as they say, very christian louboutin shoes.First up was the versus limit, louboutin boots the cheaper christian louboutin line. Girls stomped christian louboutin 2010 out with backcombed tresses bearing turquoise leather trousers, christian louboutin uk sale patchwork pullover and blonde fur sliced into stoles and active jackets. christian boots uk But this aggressive hell’s angels look almost seemed a caricature of the christian louboutin boots christian louboutin boots comfort. louboutin sandals Next up was gianni christian louboutin, buy christian louboutin the main collection