3

I have a MySQL database with a date_of_birth field. The data stored in this field is of the format: MM-DD-YYYY. I need to compute age for this field so I must compare and take the difference of CurDate().

To do this I have written the following:

select FLOOR((DATE_FORMAT(curdate(), '%m-%d-%Y') - date_of_birth)/10000) 
from patients

So I would expect it to be comparing the CurDate() of MM-DD-YYYY minus DOB of MM-DD-YYYY. However, for my one test case it continues to return a -1. I've got this working fine with MSSQL but it seems MySQL is a bit more picky/less user friendly.

Am I missing something here? Whats the deal, please help!

Thanks

2
  • Eh, why aren't you using mysql date format? Storing it in native is just... Inefficient. Is it possible to change your table structure? Commented Feb 9, 2012 at 19:53
  • It's not my design Vyk. It was the idiots before me that knew nothing about MySQL. So I'm stuck with it but I got it working now. Commented Feb 9, 2012 at 20:07

2 Answers 2

4
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(date_of_birth, '%Y') - 
      (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(date_of_birth, '00-%m-%d')) 
       AS age 
FROM patients

http://ma.tt/2003/12/calculate-age-in-mysql/

ps: if your date is in another format (but I would advise you to keep it 'native' format YYYY-MM-DD) then

SELECT 
 DATE_FORMAT(NOW(), '%Y') - 
              DATE_FORMAT(STR_TO_DATE(date_of_birth, '%m-%d-%Y'), '%Y')
- (DATE_FORMAT(NOW(), '00-%m-%d') <
              DATE_FORMAT(STR_TO_DATE(date_of_birth, '%m-%d-%Y'), '00-%m-%d')) 
   AS age 
FROM patients
Sign up to request clarification or add additional context in comments.

4 Comments

Still just returning NULLs. My date_of_birth is stored in format MM-DD-YYYY - this is the stumbling block.
Thanks Cheery! Was attempting to make those same modifictations.. As for it being stored in an unconventional format for MySQL, well that cannot be helped at this point :( Gotta deal with it.
@Encryption You can easily update all the data in DB to the specified format with the couple of requests (including altering of the table's structure). The best way is to format date at output.
I realize that but this is connected to a web interface. Would require interface changes as to how that information is displayed and saved. Not something I'm going to deal with right now(all I need was a report, heh).
2

You can use datediff()

SELECT DATEDIFF(CURDATE(),date_of_birth) AS Age

3 Comments

Doesn't work. Just returns NULLs. Even tried using DATEDIFF(DATE_FORMAT(curdate(), '%m-%d-%Y'), date_of_birth)
DATEDIFF returns difference in days
do the date_format on date_of_birth and see if that works. I know with oracle, even fields stored as dates can be quirky and I had to cast it as a date anyway. Try masking both dates as well so that the db is comparing apples to apples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.