从 Oracle SQL 中的当前日期减去 30 年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28748104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtracting 30 Years from Current Date in Oracle SQL
提问by Matt W
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.
我需要编写一个查询,其中我选择所有出生日期在 30 年前的人。不幸的是,由于我使用的是 Oracle,因此无法使用该DATEADD()
功能。
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';
回答by Habib
Use Add_MONTHS
to add(- 12 * 30)
.
使用Add_MONTHS
来添加(- 12 * 30)
。
SELECT Name, DOB
FROM Employee
WHERE DOB <= ADD_MONTHS(SYSDATE, -(12 * 30));
回答by Aramillo
Other way, using intervals:
其他方式,使用间隔:
SELECT Name, DOB
FROM Employee
WHERE DOB <= sysdate - interval '30' year;
回答by null
simply use ROUND((sysdate - DOB)/365)
:
只需使用ROUND((sysdate - DOB)/365)
:
SELECT Name, DOB
FROM Employee
WHERE ROUND((sysdate - DOB)/365) <= 30;