Andrés Orozco
Andrés Orozco

Reputation: 2661

SQL select by date from today

I'm studying SQL (Using MariaDB 10.2) and I'm using a huge example database I found online. It has an 'employees' table, and this one has a 'birth_date' column of 'date' type. I want to select all those employees who are more than 50 years old, for example, or maybe those who will be 25 years old on an specific date . Is there a way to do something like this?

Upvotes: 2

Views: 313

Answers (4)

Lucifer Rodstark
Lucifer Rodstark

Reputation: 216

You can use INTERVAL function which you can add or subtract Dates:

SELECT * FROM employee WHERE birth_date <= (NOW() - INTERVAL 50 YEAR);
  • NOW() -----> Present Date
  • -INTERVAL 50 YEAR -----> Subtract that Date to 50 Years

Upvotes: 0

KCCole
KCCole

Reputation: 144

Assuming t-sql, then for those over 50 today, use:

select * from employees where datediff(year, birth_date, now()) > 50

For those who will be 25 on a certain date use:

select * from employees where datediff(year, birthdate, certain_date) = 25

Upvotes: 2

Mureinik
Mureinik

Reputation: 310983

You can add or subtract intervals to dates:

SELECT *
FROM   employee
WHERE  birth_date <= NOW() - INTERVAL 50 YEAR

Upvotes: 4

Niket Joshi
Niket Joshi

Reputation: 748

Use this

SELECT * FROM myTable WHERE DATE(myDate) = DATE(NOW())

This is for MySql DB

Upvotes: 0

Related Questions