如何将 Sql Server 2008 DateTimeOffset 转换为 DateTime
我希望将具有 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`.
干杯:)
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
.
eg.
CreatedOn: 2008-12-19 17:30:09.0000000 +11:00
that will get converted to
CreatedOn: 2008-12-19 06:30:09.0000000
or
CreatedOn: 2008-12-19 06:30:09.0000000 + 00:00 -- that's a `DATETIMEOFFSET`, but `UTC`.
Cheers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用几乎任何样式进行转换都会导致 datetime2 值转换为 UTC。
此外,从 datetime2 到 datetimeoffset 的转换只需将偏移量设置为
+00:00
,如下所示,因此这是从Datetimeoffset(offset!=0) 转换的快速方法。 > 到
日期时间偏移(+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 fromDatetimeoffset(offset!=0)
toDatetimeoffset(+00:00)
我会使用内置的 SQL 选项:
I'd use the built in SQL option:
我知道这是一个老问题,但是,如果您想将 DateTimeOffset 转换为 DateTime,我认为您需要考虑要转换的服务器的时区。如果您只是执行 CONVERT(datetime, @MyDate, 1) 您只会丢失时区,这可能会导致转换不正确。
我认为您首先需要切换 DateTimeOffset 值的偏移量,然后进行转换。
在偏移量为 -7:00 的服务器上将“2013-11-21 00:00:00.0000000 -00:00”转换为 DateTime 的结果将是 2013-11-20 17:00:00.000。通过上述逻辑,无论服务器的时区或 DateTime 值的偏移量如何,它都会转换为服务器时区的 DateTime。
我相信您需要这样做,因为 DateTime 值包含一个假设,即该值位于服务器的时区中。
I know this is an old question but, if you want to convert DateTimeOffset to a DateTime, I think you need to take into account the timezone of the server you are converting on. If you just do a CONVERT(datetime, @MyDate, 1) you will simply lose the time zone, which likely results in an incorrect conversion.
I think you first need to switch the offset of the DateTimeOffset value, then do the conversion.
The result of converting '2013-11-21 00:00:00.0000000 -00:00' to a DateTime on a server who's offset is -7:00 will be 2013-11-20 17:00:00.000. With the above logic it doesn't mater what the time zone of the server or the offset of the DateTime value, it will be converted to DateTime in the servers time zone.
I believe you need to do this because a DateTime value includes an assumption that the value is in the time zone of the server.
SQL Server 中的 DateTimeoffset(时区)转换。
SQL Server 2016 (13.x) 及更高版本
示例
结果将是
获取时区名称、偏移量和 DST 标志的完整列表:
DateTimeoffset (Timezone) conversion in SQL Server.
SQL Server 2016 (13.x) and later
Example
Result will be
Get a full list of timezone names, offsets and DST flags:
注意:如果未指定样式(此处为“126”),则转换时时区信息将被丢弃。它也可能在其他一些样式中被丢弃,我不知道——无论如何,以下内容都会正确调整 TZ 信息。请参阅CAST 和 CONVERT。
快乐的 SQL 操作。
编辑
不确定这是否重要,但是...
datetime
实际上无法存储该级别的精度/准确度。如果运行上面的代码,秒的小数部分将被截断为 3 位数字(并且精度低于此值)。与datetime2
(和datetimeoffset(7)
)相同,生成一个未截断的值:Note: The timezone information is discarded in conversion if no style ("126" here) is specified. It might also be discarded in some of the other styles, I don't know -- in any case the following correctly adjusts for the TZ information. See CAST and CONVERT.
Happy SQL'ing.
Edit
Not sure if it matters but ...
datetime
Can't actually store that level of precision/accuracy. If the above is run the fractional seconds will be truncated to 3 digits (and accuracy is less than that). The same-same withdatetime2
(anddatetimeoffset(7)
) produces a non-truncated value:从
DateTimeOffset
转换为DateTime2
(UTC 或本地)的多种方法。在 SQL Server 2019 上:
Several ways of converting from
DateTimeOffset
toDateTime2
(UTC or local).On SQL Server 2019:
为了考虑夏令时,我使用了以下内容:
注意:
time_stamp_end_of_interval
是一个 varcharIn order to account for daylight savings time, I used the following:
Note:
time_stamp_end_of_interval
is a varchar