I need to write a query in which I select all people who have a date of birth over 30 years ago. Unfortunately, as I am using Oracle I cannot use the DATEADD()
function.
I have currently got this, but obviously this isn't dynamic and won't change as the years pass:
SELECT Name, DOB
FROM Employee
WHERE DOB <= DATE '1985-01-01';
Date – dateYou can subtract a date from a date in Oracle. The result will be in days. You can also multiply by 24 to get hours and so on.
Use current_date to get today's date. In Oracle, you can subtract any number of days simply by subtracting that number from the current date. Here, since you need to subtract one day, you use current_date - 1 . Then you use the TO_DATE() function to cast the result to the column type date .
The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.
For 20 years, you can do sysdate - 20*365.25 . This will also work until 2120.
Use Add_MONTHS
to add (- 12 * 30)
.
SELECT Name, DOB
FROM Employee
WHERE DOB <= ADD_MONTHS(SYSDATE, -(12 * 30));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With