【问题标题】:SQL DATEDIFF return wrong valueSQL DATEDIFF 返回错误值
【发布时间】:2018-09-02 06:39:23
【问题描述】:

我们想计算两个DateTimeOffsets之间的差,但是,SQL返回错误的值,我们做错了什么?

DECLARE @timeInZone1 AS DATETIMEOFFSET
DECLARE @timeInZone2 AS DATETIMEOFFSET
SET @timeInZone1 = '2012-01-13 00:00:00 +1:00';
SET @timeInZone2 = '2012-01-13 23:00:00 +1:00';
SELECT DATEDIFF( day, @timeInZone1, @timeInZone2 );

差值应该是 0 但它返回 1

【问题讨论】:

  • Per the docs:“在计算返回值时不使用 startdate 或 enddate 的时区偏移分量。”我认为这是“我们实际上是根据转换后的 UTC 时间计算的”(其中2012-01-13 00:00:00 +1:00 实际上是2012-01-12 的一部分)而不是“我们忽略时区,因此当您使用同一时间时答案至少是正确的”区域一致”。考虑计算hour 的差值并除以 24;即使没有时区,这通常也是您想要的。
  • 可能是当您使用时间 '00:00:00' 时,它仅被解释为日期,然后根据文档,时区设置为 '0:00',因此1 天的差异。
  • 请注意,将 datetimeoffset 值转换为 datetime 会返回 0:select datediff( day, convert(datetime, @timeInZone1), convert(datetime, @timeInZone2));

标签: sql sql-server tsql datetime datetimeoffset


【解决方案1】:

如评论中所述,如果您投射它似乎有效。但不清楚为什么对我来说。

DECLARE @timeInZone1 AS DATETIMEOFFSET
DECLARE @timeInZone2 AS DATETIMEOFFSET
SET @timeInZone1 = '2012-01-13 00:00:00 +1:00';
SET @timeInZone2 = '2012-01-13 23:00:00 +1:00';
SELECT  @timeInZone1 as z1, @timeInZone2 as z2
      , cast(@timeInZone1 as datetime) z1d,  cast(@timeInZone2 as datetime) z2d 
      , DATEDIFF(day, @timeInZone1, @timeInZone2) as diff
      , DATEDIFF(day, cast(@timeInZone1 as datetime), cast(@timeInZone2 as datetime)) as diffdt;

z1                                 z2                                 z1d                     z2d                     diff        diffdt
---------------------------------- ---------------------------------- ----------------------- ----------------------- ----------- -----------
2012-01-13 00:00:00.0000000 +01:00 2012-01-13 23:00:00.0000000 +01:00 2012-01-13 00:00:00.000 2012-01-13 23:00:00.000 1           0

【讨论】:

  • 这并不奇怪-datetime 没有本地与 UTC 的偏移或概念。转换只是去除偏移量。在投射之前,您必须使用一个函数来考虑偏移量
【解决方案2】:

非常有趣!正如 Jeroen Mostert 评论的那样,datetimeoffset 值似乎已转换为 UTC:

select datediff(day, '2017-01-01 0:00 +1:00', '2017-01-01 1:00 +1:00') --> 1

转换为 UTC 将使第一个值 `2016-12-31 23:00' 提前一天。

但正常的datetime 值不会转换为 UTC:

select datediff(day, '2017-01-01 0:00', '2017-01-01 1:00') --> 0

生活在 UTC+1,令人惊讶的是,添加 +1:00 会得到不同的结果。毕竟+1:00是这里的默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多