How can I convert a Sql Server 2008 DateTimeOffset to a DateTime(如何将 Sql Server 2008 DateTimeOffset 转换为 DateTime)
问题描述
我希望将具有 DATETIMEOFFSET 字段的表转换为 DATETIME 字段,但通过注意偏移量来重新计算时间.这实际上将值转换为 UTC.
I'm hoping to convert a table which has a DATETIMEOFFSET field, down to a DATETIME field BUT recalculates the time by taking notice of the offset. This, in effect, converts the value to 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)
Converting using almost any style will cause the datetime2 value to be converted to UTC.
Also, conversion from datetime2 to datetimeoffset simply sets the offset at +00:00, per the below, so it is a quick way to convert from Datetimeoffset(offset!=0) to 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 Server 2008 DateTimeOffset 转换为 DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Sql Server 2008 DateTimeOffset 转换为 DateTime
基础教程推荐
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
