如何从SQL的当前日期开始减去30天?
我正在尝试从数据库中的日期列中减去30天,并将其用作的条件 表示例:
FACT_DAY |
---|
2022-05-20 |
2022-05-20 |
2022-04-15 |
2022-05-28 |
我的试验:
where pr.fact_day between current_date and current_date - 30
预期输出是在今天约会前30天的行中获得所有信息
I am trying to subtract 30 days from a date column in my database and use that as a condition in my where
but I can't get it to work
table example:
Fact_day |
---|
2022-05-20 |
2022-05-20 |
2022-04-15 |
2022-05-28 |
My trial:
where pr.fact_day between current_date and current_date - 30
Expected output is to get me all info in the rows that are 30 days before today's date
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用DATEADD将天数减去所需的日期
SQL Server Dateadd()功能
编号。迄今为止要添加的间隔数。可能是正(将来要获得日期)或负面(过去的日期)
示例:
https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
You can use DATEADD to subtract days to a given date
SQL Server DATEADD() Function
number Required. The number of interval to add to date. Can be positive (to get dates in the future) or negative (to get dates in the past)
Example:
https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
假设Postgres,来自比较操作员:
因此,这是:
在current_date和current_date之间 - 30
需要以下:
purrent_date -30和current_date
,除非您使用
Symmetric
。Assuming Postgres, from Comparison operators:
So this:
between current_date and current_date - 30
needs to be this:
between current_date - 30 and current_date
unless you use
SYMMETRIC
.