[英]datetimeoffset vs datetime2 for UTC on SQL Server
Are there any benefits to storing a UTC timestamp in a datetimeoffset field vs datetime2?在 datetimeoffset 字段与 datetime2 中存储 UTC 时间戳有什么好处吗? It seems they're essentially the same.看来它们本质上是一样的。
+------------------------------------+-----------------------------+
| datetimeoffset | datetime2 |
|------------------------------------+-----------------------------|
| 2021-02-12 16:48:11.0677934 +00:00 | 2021-02-12 16:48:11.0677934 |
+------------------------------------+-----------------------------+
The datetimeoffset
data type will allow comparison between different offsets of the same time. datetimeoffset
数据类型将允许在同一时间的不同偏移量之间进行比较。 eg:例如:
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).
If you are storing only UTC values (where the offset is always zero), you can save storage space with datetime2
.如果您只存储 UTC 值(偏移量始终为零),则可以使用datetime2
节省存储空间。 datetimeoffset
requires 10 bytes of storage whereas datetime
needs 8 bytes for precision 5 or greater, 7 bytes for precision 3-4, and 6 bytes for precision 2 or less. datetimeoffset
需要 10 个字节的存储空间,而datetime
需要 8 个字节(精度 5 或更高),7 字节(精度 3-4)和 6 个字节(精度 2 或更低)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.