Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL datediff - People older than

Tags:

sql

sql-server

I have an SQL table called member in which I have member information and in that information, their birth date.

I need to get a list of all the people that are more than 25 years old. For now, I got this :

SELECT * FROM members
WHERE DATEDIFF(year, birthday, GETDATE() ) > 25

The only thing is that it doesn't take into consideration all the people that turned 25 this year so far...

How do I add to this everyone that turned 25 in January and February of this year as well ?

Would someone be able to help me out ?

Thanks !

like image 313
Denis B Avatar asked May 19 '26 04:05

Denis B


2 Answers

Don't use datediff(). Just subtract the years from the current date:

where birthday < dateadd(year, -25, getdate())
like image 108
Gordon Linoff Avatar answered May 22 '26 01:05

Gordon Linoff


Try adding 25 years to their birthday and then comparing to now. If <=, then they are older than 25.

SELECT * 
    FROM members
    WHERE DATEADD(year, 25, birthday) <= getdate()
like image 32
Chris Steele Avatar answered May 22 '26 00:05

Chris Steele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!