oracle find people whose 65th birthday is 30 days in the future

时间:2016-04-15 14:54:17

标签: sql oracle

From oracle database I am trying to capture people who will turn 65 years old in 30 days in the future.

Thanks!

1 个答案:

答案 0 :(得分:2)

select *
from   your_table
where  birthdate between add_months(sysdate, -12*65) and add_months(sysdate, -12*65) + 30

Logically, you want to add 65 years to birthdate and then check if it's between today and 30 days from today. Oracle only has an add_months function, no add_years, so you need to multiply by 12. Finally, I moved the add_months to the right side; if the optimizer is smart, it will do the calculation only once, not once per row - and not applying a function to "birthdate" allows use of an index, if you have one defined on that column.