If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Calculating age in MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-07, 07:53
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Calculating age in MySQL

Can someone tell me if there is some function that will take the current year minus a person's date of birth to get their age? If someone could point me towards some info, I would be grateful. I don't see how an aggregate function could do this with the date format.

Thanks..
Reply With Quote
  #2 (permalink)  
Old 06-20-07, 08:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
aggregate functions are used to perform a calculation over the values in a column -- you're right, not the thing for this problem

let's say you were born on august 13th, 1971

this is 2007, so 2007 - 1971 = 36, and you are 36 years old today, right?

wrong
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-20-07, 08:31
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
mysql> select  date_format(now(),'%Y') - substring('19630805',1,4) -
        if (date_format(now(),'%m%d') < substring('19630805',5,4),
                1, 0 ) as age
+------+
| age  |
+------+
|   43 |
+------+
Obviously the 19630805 in this example will need to be a variable (an 8 digit string containing the birthday) and I am mixing types but it's close to what you are after.

It's a terrible thing to suddenly come across your age.

Mike

Last edited by mike_bike_kite; 06-20-07 at 08:42.
Reply With Quote
  #4 (permalink)  
Old 06-20-07, 16:04
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Thanks for the help. I was thinking that maybe the date would be better handled by php.
Reply With Quote
  #5 (permalink)  
Old 06-20-07, 19:38
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
See this tutorial in the mysql manual - http://dev.mysql.com/doc/refman/5.0/...culations.html
Reply With Quote
  #6 (permalink)  
Old 06-20-07, 20:40
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Quote:
Originally Posted by dbmab
See this tutorial in the mysql manual - http://dev.mysql.com/doc/refman/5.0/...culations.html
Thank you dbmab. That certainley seems as though that would do the trick!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On