如何从SQL的当前日期开始减去30天?

发布于 2025-02-10 22:48:17 字数 399 浏览 0 评论 0原文

我正在尝试从数据库中的日期列中减去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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2025-02-17 22:48:17

您可以使用DATEADD将天数减去所需的日期

SQL Server Dateadd()功能

DATEADD(interval, number, date)

编号。迄今为止要添加的间隔数。可能是正(将来要获得日期)或负面(过去的日期)

示例:

SELECT DATEADD(day, -30, '2017/08/25') AS 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

DATEADD(interval, number, date)

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:

SELECT DATEADD(day, -30, '2017/08/25') AS DateAdd;

https://www.w3schools.com/sql/func_sqlserver_dateadd.asp

深陷 2025-02-17 22:48:17

假设Postgres,来自比较操作员

谓词之间简化了范围测试:

x和y

之间

等于

a> = x和a< = y

请注意,将端点值处理为范围内的端点值。在对称之间就像在两者之间,除了不要求左侧的论点,并且不小于或等于右侧的论点。如果不是这样,那两个参数会自动互换,因此始终暗示非空范围。

因此,这是:

在current_date和current_date之间 - 30

需要以下:

purrent_date -30和current_date

除非您使用Symmetric

Assuming Postgres, from Comparison operators:

The BETWEEN predicate simplifies range tests:

a BETWEEN x AND y

is equivalent to

a >= x AND a <= y

Notice that BETWEEN treats the endpoint values as included in the range. BETWEEN SYMMETRIC is like BETWEEN except there is no requirement that the argument to the left of AND be less than or equal to the argument on the right. If it is not, those two arguments are automatically swapped, so that a nonempty range is always implied.

So this:

between current_date and current_date - 30

needs to be this:

between current_date - 30 and current_date

unless you use SYMMETRIC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文