【问题标题】:datetimeoffset vs datetime2 for UTC on SQL ServerSQL Server 上 UTC 的 datetimeoffset 与 datetime2
【发布时间】:2021-02-12 17:08:53
【问题描述】:

将 UTC 时间戳存储在 datetimeoffset 字段与 datetime2 相比有什么好处吗?看来它们本质上是一样的。

+------------------------------------+-----------------------------+
| datetimeoffset                     | datetime2                   |
|------------------------------------+-----------------------------|
| 2021-02-12 16:48:11.0677934 +00:00 | 2021-02-12 16:48:11.0677934 |
+------------------------------------+-----------------------------+

【问题讨论】:

  • 相同精度的datetime2在存储方面会更小。如果您不打算存储除 UTC 以外的任何内容,请使用 datetime2
  • 日期时间偏移将允许在同一时间的不同偏移之间进行比较。例如CAST('2021-02-12 15:48:11.0677934 -01:00' AS datetimeoffset) = CAST('2021-02-12 16:48:11.0677934 +00:00' AS datetimeoffset)

标签: sql-server datetime utc datetimeoffset


【解决方案1】:

datetimeoffset 数据类型将允许在同一时间的不同偏移量之间进行比较。例如:

SELECT 'equal'
WHERE
    CAST('2021-02-12 15:48:11.0677934 -01:00' AS datetimeoffset) = CAST('2021-02-12 16:48:11.0677934 +00:00' AS datetimeoffset).

如果您只存储 UTC 值(偏移量始终为零),您可以使用 datetime2 节省存储空间。 datetimeoffset 需要 10 个字节的存储空间,而 datetime 需要 8 个字节(精度为 5 或更高)、7 个字节(精度为 3-4)和 6 个字节(精度为 2 或更低)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多