问题描述
在 datetimeoffset 字段中存储 UTC 时间戳与 datetime2 相比有什么好处吗?看起来它们本质上是一样的。
+------------------------------------+-----------------------------+
| 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 值(其中偏移量始终为零),您可以使用 datetime2
节省存储空间。 datetimeoffset
需要 10 个字节的存储空间,而 datetime
需要 8 个字节(精度 5 或更高)、7 个字节(精度 3-4)和 6 个字节(精度 2 或更低)。