Reputation: 2661
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
Reputation: 216
You can use INTERVAL function which you can add or subtract Dates:
SELECT * FROM employee WHERE birth_date <= (NOW() - INTERVAL 50 YEAR);
Upvotes: 0
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
Reputation: 310983
You can add or subtract interval
s to date
s:
SELECT *
FROM employee
WHERE birth_date <= NOW() - INTERVAL 50 YEAR
Upvotes: 4
Reputation: 748
Use this
SELECT * FROM myTable WHERE DATE(myDate) = DATE(NOW())
This is for MySql DB
Upvotes: 0