SQL Server 上 UTC 的 datetimeoffset 与 datetime2

wpd*_*mon 7 sql-server datetime utc datetimeoffset

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

+------------------------------------+-----------------------------+
| datetimeoffset                     | datetime2                   |
|------------------------------------+-----------------------------|
| 2021-02-12 16:48:11.0677934 +00:00 | 2021-02-12 16:48:11.0677934 |
+------------------------------------+-----------------------------+
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 15

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

SELECT 'equal'
WHERE
    CAST('2021-02-12T15:48:11.0677934-01:00' AS datetimeoffset) = CAST('2021-02-12T16:48:11.0677934+00:00' AS datetimeoffset).
Run Code Online (Sandbox Code Playgroud)

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