oracle SQL 过滤超过 30 天的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20909333/
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
SQL to filter for records more than 30 days old
提问by user2917239
Suppose I have the following query:
假设我有以下查询:
select customer_name, origination_date
where origination_date < '01-DEC-2013';
I would like to select all customers that have an origination date older than 30 days. Is there a way in SQL (oracle, if specifics needed) to specify it in a more dynamic approach than manually entering the date so that I don't need to update the query every time I run it?
我想选择所有起始日期超过 30 天的客户。SQL(oracle,如果需要的话)中有没有办法以比手动输入日期更动态的方法指定它,这样我就不需要每次运行时都更新查询?
Thanks!
谢谢!
回答by ProVega
Sure try something like this:
当然尝试这样的事情:
select customer_name, origination_date where
origination_date >= DATEADD(day, -30, GETUTCDATE());
This basically says where the origination_date is greater or equal to 30 days from now. This works in Microsoft SQL, not sure but there is probably a similar function on Oracle.
这基本上表示 origination_date 从现在起大于或等于 30 天。这适用于 Microsoft SQL,不确定,但在 Oracle 上可能有类似的功能。
回答by Ismail Hawayel
in Oracle, when you subtract dates, by default you get the difference in days, e.g.
在 Oracle 中,当您减去日期时,默认情况下您会得到天数差异,例如
select * from my_table where (date_1 - date_2) > 30
should return the records whose date difference is greater than 30 days. To make your query dynamic, you parameterize it, so instead of using hard coded date values, you use:
应该返回日期差异大于 30 天的记录。为了使您的查询动态化,您可以对其进行参数化,因此您不使用硬编码的日期值,而是使用:
select * from my_table where (:date_1 - :date_2) > :threshold
If you are using oracle sql developer to run such a query, it will pop up a window for you to specify the values for your paramteres; the ones preceded with colon.
如果你使用oracle sql developer来运行这样的查询,它会弹出一个窗口让你指定参数的值;前面有冒号的那些。