gpt4 book ai didi

sql - INTERVAL '1' DAY 是否始终等于 INTERVAL '24' HOURS?

转载 作者:行者123 更新时间:2023-12-05 03:53:32 28 4
gpt4 key购买 nike

直观上,这两个间隔代表相同的时间量。但区别在于夏令时的变化,在这种情况下,“1 天”在 Spring 可能表示“23 小时”,在秋季可能表示“25 小时”。

我用 PostgreSQL 进行了测试,这两个间隔并不意味着相同的时间量:

set timezone TO 'CET';
SELECT timestamp with time zone'2020-03-29 0:00 Europe/Bratislava' + INTERVAL '1' DAY,
timestamp with time zone'2020-03-29 0:00 Europe/Bratislava' + INTERVAL '24' HOUR;

返回这个:

2020-03-29T22:00:00.000Z    2020-03-29T23:00:00.000Z

客户端的时区是 UTC,这就是返回值采用 UTC 的原因。但重要的是它们并不相同。

我也尝试过使用 MySQL,但在我看来它不支持时区,只支持时区偏移。时区偏移没有 DST 更改,因此一天始终是 24 小时。

另一方面,Apache Calcite 支持许多 SQL 实现,例如 Apache Drill、Apache Flink、Apache Beam 等等,将间隔文字表示为 java 的 BigDecimal:日-秒间隔被转换以毫秒为单位,假设天始终为 24 小时。

我的问题是:根据 SQL 标准,哪种方法是正确的?

编辑:

检查了更多数据库:

  • Oracle:SELECT INTERVAL '1' DAY FROM DUAL 返回 +01 00:00:00。向 2020-03-29 0:00 CET 添加 1 天或 24 小时提供相同的结果:添加 24 小时。
  • SQL ServerDB2:据我所知,仅支持时区偏移。与 MySql 的情况相同:它们不支持 DST 更改的时区。

结论:PostgreSQL 似乎是唯一有 1 天不同于 24 小时的异常(exception)。

最佳答案

我认为您的答案在于此资源:https://www.postgresql.org/docs/9.1/functions-datetime.html

When adding an interval value to (or subtracting an interval valuefrom) a timestamp with time zone value, the days component advances(or decrements) the date of the timestamp with time zone by theindicated number of days. Across daylight saving time changes (withthe session time zone set to a time zone that recognizes DST), thismeans interval '1 day' does not necessarily equal interval '24 hours'.For example, with the session time zone set to CST7CDT, timestamp withtime zone '2005-04-02 12:00-07' + interval '1 day' will producetimestamp with time zone '2005-04-03 12:00-06', while adding interval'24 hours' to the same initial timestamp with time zone producestimestamp with time zone '2005-04-03 13:00-06', as there is a changein daylight saving time at 2005-04-03 02:00 in time zone CST7CDT.

关于sql - INTERVAL '1' DAY 是否始终等于 INTERVAL '24' HOURS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61505068/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com