Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an age from a D.O.B field in MySQL?

Tags:

date

select

mysql

I need to calculate the age of a "customer" from their date of birth.

I have tried to use the following:

DATEDIFF(year, customer.dob, "2010-01-01");

But it does not seem to work.

Any ideas? I KNOW it is going to be something simple!

Thanks

like image 624
aadersh patel Avatar asked Mar 28 '10 17:03

aadersh patel


1 Answers

SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age 
like image 62
Bryan Denny Avatar answered Sep 22 '22 15:09

Bryan Denny