user2948897
user2948897

Reputation: 169

SQL -- Calculating Age from Birthday Record

The following is my attempt at calculating the birthday from the person who is the oldest in my table:

select firstname, 
datediff(year(curtime()), min(year(birthday))) 
from TABLE_A ;

The query returns NULL.

I suppose that I have forgotten how this is done. I am more than likely missing something simple.

Upvotes: 0

Views: 362

Answers (3)

M.Ali
M.Ali

Reputation: 69524

Mysql

SELECT year(NOW())-  year(birthday))  
FROM table WHERE condition 
ORDER BY dateField ASC LIMIT 1

Sql Server

SELECT TOP 1 DATEDIFF(YEAR, birthday, GETDATE())
FROM Table_A
ORDER BY birthday ASC

Upvotes: 1

Matt
Matt

Reputation: 1812

SELECT YEAR(TIMEDIFF(NOW(),MIN(birthday))) FROM TABLE_A

or

SELECT TIMESTAMPDIFF(YEAR, MIN(birthday), NOW()) FROM TABLE_A

This list of MySQL date and time functions will be useful.

Upvotes: 0

eggyal
eggyal

Reputation: 125865

The arguments to DATEDIFF() should be dates; whereas the result of YEAR() is an integer representing only a date's year. If you are looking to obtain the age of the eldest person in TABLE_A, you can use MySQL's TIMESTAMPDIFF() function:

SELECT TIMESTAMPDIFF(YEAR, MIN(birthday), CURRENT_DATE) FROM TABLE_A

Upvotes: 1

Related Questions