SQL / Oracle 中的 INTERVAL '182' DAY(3) 是什么意思?

What does INTERVAL '182' DAY(3) mean in SQL / Oracle?

试图了解 SQL 中的 INTERVAL 和 DAY(3) 用法,因为我遇到过它并且需要了解它的含义。它位于 WHERE 子句中,如下所示

WHERE POSTING_DATE > CURRENT_DATE - INTERVAL '182' DAY(3)

需要帮助,谢谢!

From the documentation:

INTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes, and seconds. This data type is useful for representing the precise difference between two datetime values.

Specify this data type as follows:

INTERVAL DAY [(day_precision)]
TO SECOND [(fractional_seconds_precision)]

where

day_precision is the number of digits in the DAY datetime field. Accepted values are 0 to 9. The default is 2.
...

所以INTERVAL '182' DAY(3)定义了182天的间隔。您需要指定 (3) 部分,因为默认精度 2 不允许超过 99 天;如果没有精度覆盖,它将出错:

select INTERVAL '182' DAY from dual;

ORA-01873: the leading precision of the interval is too small

但是有了它你会得到一个有效值:

select INTERVAL '182' DAY(3) from dual;

INTERVAL'182'DAY(3)
-------------------
+182 00:00:00

计算 CURRENT_DATE - INTERVAL '182' DAY(3) 将为您提供 182 天前的当前时间(根据您的会话时区,因为您使用的是 current_date 而不是 sysdate)。您的查询将查找 POSTING_DATE 比 182 天前更新的行。