datetimeoffset 与 datetime2

问题描述 投票:0回答:1

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

+------------------------------------+-----------------------------+
| datetimeoffset                     | datetime2                   |
|------------------------------------+-----------------------------|
| 2021-02-12 16:48:11.0677934 +00:00 | 2021-02-12 16:48:11.0677934 |
+------------------------------------+-----------------------------+
sql-server datetime utc datetimeoffset
1个回答
12
投票

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).

如果您仅存储 UTC 值(其中偏移量始终为零),则可以使用

datetime2
节省存储空间。
datetimeoffset
需要 10 个字节的存储空间,而
datetime2
对于精度 5 或更高的精度需要 8 个字节,对于精度 3-4 需要 7 个字节,对于精度 2 或更低需要 6 个字节。

© www.soinside.com 2019 - 2024. All rights reserved.