我希望将具有 DATETIMEOFFSET
字段的表转换为 DATETIME
字段,但通过注意偏移量来重新计算时间。实际上,这会将值转换为 UTC
。
例如。
CreatedOn: 2008-12-19 17:30:09.0000000 +11:00
将被转换为
CreatedOn: 2008-12-19 06:30:09.0000000
或
CreatedOn: 2008-12-19 06:30:09.0000000 + 00:00 -- that's a `DATETIMEOFFSET`, but `UTC`.
干杯:)
最佳答案
使用几乎任何样式进行转换都会导致 datetime2 值转换为 UTC。
此外,从 datetime2 到 datetimeoffset 的转换只需设置 +00:00
处的偏移量,如下所示,因此这是从 Datetimeoffset(offset!=0)
转换为 Datetimeoffset(+00:00)
的快速方法
declare @createdon datetimeoffset
set @createdon = '2008-12-19 17:30:09.1234567 +11:00'
select CONVERT(datetime2, @createdon, 1)
--Output: 2008-12-19 06:30:09.12
select convert(datetimeoffset,CONVERT(datetime2, @createdon, 1))
--Output: 2008-12-19 06:30:09.1234567 +00:00
关于sql - 如何将 Sql Server 2008 DateTimeOffset 转换为 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4953903/