两个时间戳之间的 Oracle 查询不返回记录

Oracle query between two timestamps not returning records

我正在尝试获取数据库中更新日期介于系统时间戳 -15 分钟和当前系统时间戳之间的所有记录。

所以我做的是:

and (CAST (update_date AS TIMESTAMP)  at time zone 'UTC') BETWEEN  (cast((systimestamp - interval '15' minute) at time zone 'UTC' as timestamp)) AND  (cast((systimestamp) at time zone 'UTC' as timestamp))

如果我将它们分开用于特定记录:

(CAST (update_date AS TIMESTAMP)  at time zone 'UTC') -> 26-APR-18 01.10.16.000000000 AM UTC
(cast((systimestamp - interval '15' minute) at time zone 'UTC' as timestamp)) -> 26-APR-18 12.57.04.136000000 AM
(cast((systimestamp) at time zone 'UTC' as timestamp)) -> 26-APR-18 01.12.04.136000000 AM

基本上第一个在另外两个之间,所以查询应该 return 一条记录,但它没有。有帮助吗?

SYSTIMESTAMP returns 一个 TIMESTAMP WITH TIME ZONE 值。

假设 update_date 也是一个 TIMESTAMP WITH TIME ZONE 你根本不需要任何转换,只需 运行

update_date BETWEEN SYSTIMESTAMP - interval '15' minute AND SYSTIMESTAMP

比较总是在内部以 UTC 进行,参见 Datetime and Interval Arithmetic:

Oracle Database performs all timestamp arithmetic in UTC time. For TIMESTAMP WITH LOCAL TIME ZONE data, Oracle Database converts the datetime value from the database time zone to UTC and converts back to the database time zone after performing the arithmetic. For TIMESTAMP WITH TIME ZONE data, the datetime value is always in UTC, so no conversion is necessary.

因为你在 PST 中有 DATE 个值(注意,在这种情况下你如何处理夏令时?)你会 运行

FROM_TZ(CAST(update_date AS TIMESTAMP), 'PST') BETWEEN SYSTIMESTAMP - interval '15' minute AND SYSTIMESTAMP