I have stored date of birth of my users into my database..
I want to calculate their age according to the database dob field...
Please help me..

Thanks..

Recommended Answers

All 14 Replies

SELECT DATE_FORMAT( FROM_DAYS( DATEDIFF( NOW( ) , "1978-03-28" ) ) , '%Y' ) +0 AS age

Even this works. Just replace the date (1978-03-28) with the date of birth column.

Hi Naveen...

I have done a code with your reference..
But i got error in mysql:

Error
SQL query:

"SELECT fname,date,curdate(),(YEAR(curdate())-YEAR(date))-(RIGHT(curdate(),5)<RIGHT(date,5)) AS age FROM date_profiles ORDER BY fname";

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT fname,date,curdate(),(YEAR(curdate())-YEAR(date))-(RIGHT

date is a mysql reserved keyword. You should use `` around reserved keyword fields to make them work.

date is a mysql reserved keyword. You should use `` around reserved keyword fields to make them work.

If dont mind...
Please edit my code and resend...
Thank you...

replace date with `date`.

date is my database field, and i declared it as varchar...
Is it a problem?

Yes. date is a mysql keyword and whatever the datatype is, its still a problem.

Yep..
I changed my date to date1..
Still i got the error...

Then maybe something is wrong with your query.
I tried it and it worked !

SELECT uname,datej,curdate(),(YEAR(curdate())-YEAR(datej))-(RIGHT(curdate(),5)<RIGHT(datej,5)) AS age FROM members ORDER BY uname
commented: smart answer +1

i got the correct output...
What my mistake is:
i have declared dob with varchar,after i changed it to date,correct output came...

Thank you naveen....

Great! :)

Hello nav..
In the above code CURDATE is database field or its todays date...tel me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.