SQL 在sql中从当前日期获取过去90天的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16479010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:09:21  来源:igfitidea点击:

fetch records of last 90 days from current date in sql

sqloracledate

提问by Anas

Hi am using below query to retrieve records last 90 days from current date but its not working. please help me.

您好,我正在使用下面的查询来检索从当前日期起过去 90 天的记录,但它不起作用。请帮我。

SELECT * 
FROM adhoc_request 
WHERE DATEDIFF(("d", Crdate, current_date()) < 90);

回答by RichardTheKiwi

SELECT * 
FROM adhoc_request 
WHERE Crdate >= sysdate-90

SYSDATEincludes the time portion, e.g. "04-13-2013 09:45:51"

SYSDATE包括时间部分,例如“04-13-2013 09:45:51”

So in effect, the query run on "04-13-2013 09:45:51" translates to

因此,实际上,在“04-13-2013 09:45:51”上运行的查询转换为

WHERE Crdate >= "2013-01-13 09:45:51"

David's suggestion works more predictably and is in most cases more correct, which first truncates SYSDATE to return just today's date before taking 90 days off it, i.e.

David 的建议更可预测,并且在大多数情况下更正确,它首先截断 SYSDATE 以返回今天的日期,然后再将其关闭 90 天,即

SELECT * 
FROM adhoc_request 
WHERE Crdate >= TRUNC(sysdate)-90

回答by TechDo

Please try below query for oracle

请尝试以下查询 oracle

SELECT 
    * 
FROM 
    adhoc_request 
WHERE 
    trunc(sysdate-Crdate)<90

回答by gbn

select *
from adhoc_request
where Crdate < DATEADD("d", -90, current_date()) < 90);

Some notes:

一些注意事项:

  • Never use a function on a predicate in a WHERE clause. Note CrDate is alone in my code
  • DATEADD will extract exactly 90 days including time. DATEDIFF counts midnight boundaries
  • 切勿在 WHERE 子句中的谓词上使用函数。注意 CrDate 在我的代码中是单独的
  • DATEADD 将准确提取 90 天(包括时间)。DATEDIFF 计算午夜边界

回答by Art

 Where trunc(Crdate) BETWEEN (trunc(sysdate)-INTERVAL '90' DAY) AND trunc(sysdate)