Adam
Adam

Reputation: 20882

MySQL convert date to age in a Select Statement

I'm using the following Select Statement to get a users birthdate and other info. I wanted to see if I could get the date converted to an age in the returned data - rather then convert it in PHP.

Date format in Database is: 1979-04-26

SELECT profile_username,profile_gender,profile_birthdate FROM loginTable WHERE id=1000;

Current return is like

Adam,Male,1979-04-26

Can I get back:

Adam,Male,34

Upvotes: 0

Views: 101

Answers (1)

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

SELECT profile_username,profile_gender,
DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(profile_birthdate, '%Y') - 
(DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(profile_birthdate, '00-%m-%d')) 
AS age
FROM loginTable WHERE id=1000;

SAMPLE FIDDLE

Upvotes: 2

Related Questions