Oracle SQL Where 子句查找超过 30 天的日期记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3860295/
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
Oracle SQL Where clause to find date records older than 30 days
提问by anwarma
I want to find records in a (Oracle SQL) table using the creation date field where records are older than 30 days. It would be nice to find records using a operators like > but if anyone can suggest quick SQL where clause statement to find records older than 30 days that would be nice. Please suggest Oracle syntax as that is what I am using.
我想使用记录超过 30 天的创建日期字段在(Oracle SQL)表中查找记录。使用像 > 这样的运算符查找记录会很好,但是如果有人可以建议使用快速 SQL where 子句语句来查找超过 30 天的记录,那就太好了。请建议 Oracle 语法,因为这是我正在使用的。
回答by OMG Ponies
Use:
用:
SELECT *
FROM YOUR_TABLE
WHERE creation_date <= TRUNC(SYSDATE) - 30
SYSDATE returns the date & time; TRUNCresets the date to being as of midnight so you can omit it if you want the creation_date
that is 30 days previous including the current time.
SYSDATE 返回日期和时间;TRUNC将日期重置为午夜,因此如果您想要creation_date
包括当前时间在内的 30 天前的日期,则可以省略它。
Depending on your needs, you could also look at using ADD_MONTHS:
根据您的需要,您还可以考虑使用ADD_MONTHS:
SELECT *
FROM YOUR_TABLE
WHERE creation_date <= ADD_MONTHS(TRUNC(SYSDATE), -1)