Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract years from sysdate

Tags:

sql

oracle

What would be the expression to check if someone's 20 years or over without hard-coding the date?

In the SQL

 SELECT student_fname   FROM students  WHERE dob<'05-MAR-1995'; 

I was thinking about using SYSDATE but then I don't know the syntax to subtract 20 years.

like image 904
redCodeAlert Avatar asked Mar 05 '15 22:03

redCodeAlert


People also ask

How do you subtract year from Sysdate?

add_months adds (or subtracts) months so this subtracts 12*20 months to subtract 20 years.

How do I subtract years from a date in SQL?

We can use DATEADD() function like below to Subtract Years from DateTime in Sql Server. DATEADD() functions first parameter value can be year or yyyy or yy, all will return the same result.

How do I add 1 year to Sysdate?

I found doing the following always returns the correct date: (to_date('02-28','MM-DD') - 1) + interval '1' year + 1 returns 02-28, even if the next year is a leap year, and if you put in 02-29, it will return 03-01 on non-leap years.

Can you subtract dates in 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.


1 Answers

WHERE dob < add_months( trunc(sysdate), -12*20 ); 

would work assuming that you want to ignore the time component of sysdate.

like image 166
Justin Cave Avatar answered Sep 18 '22 20:09

Justin Cave