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.
add_months adds (or subtracts) months so this subtracts 12*20 months to subtract 20 years.
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.
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.
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.
WHERE dob < add_months( trunc(sysdate), -12*20 );
would work assuming that you want to ignore the time component of sysdate
.
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