Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting 30 Years from Current Date in Oracle SQL

Tags:

sql

oracle

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';
like image 764
Matt W Avatar asked Feb 26 '15 16:02

Matt W


People also ask

Can you subtract dates in SQL Oracle?

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.

How do I subtract dates in Oracle?

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 .

How do I subtract a date from today's date in SQL?

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.

How do you subtract year from Sysdate?

For 20 years, you can do sysdate - 20*365.25 . This will also work until 2120.


1 Answers

Use Add_MONTHS to add (- 12 * 30).

SELECT Name, DOB
FROM Employee
WHERE DOB <= ADD_MONTHS(SYSDATE, -(12 * 30));
like image 142
Habib Avatar answered Sep 29 '22 19:09

Habib