N
N
nezzard2019-07-12 17:49:58
MySQL
nezzard, 2019-07-12 17:49:58

How to make sql selection by age having only date of birth?

Good afternoon, please tell me how to make a choice of clients, for example, who are 50 years old.
A solution like
YEAR(DOB) = (YEAR(NOW()) - 50) Doesn't work because I need to be sure that the person is 50 full years old.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nezzard, 2019-07-12
@nezzard

50 = YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(DOB) DAY))

R
Ruslan., 2019-07-12
@LaRN

You can try this:
select datediff(yy, DOB, NOW())

K
Konstantin Tsvetkov, 2019-07-12
@tsklab

Age:

DATEDIFF( Year, BirthDay, ISNULL( DeathDay, GETDATE())) 
+ ( SIGN ( DATEDIFF ( DAY, BirthDay, DATEADD ( YEAR, YEAR( BirthDay ) 
- YEAR( ISNULL( DeathDay, GETDATE())), ISNULL( DeathDay, GETDATE())))) - 1 ) / 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question