SQL 服务器上 UTC 的 datetimeoffset 与 datetime2

datetimeoffset vs datetime2 for UTC on SQL Server

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

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

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 值(其中偏移量始终为零),则可以节省存储空间 space,其中 datetime2. datetimeoffset 需要 10 个字节的存储空间,而 datetime 需要 8 个字节精度为 5 或更高,精度为 3-4 为 7 个字节,精度为 2 或更低为 6 个字节。