如何将Sql Server 2008 DateTimeOffset转换为DateTime [英] How can I convert a Sql Server 2008 DateTimeOffset to a DateTime

查看:1931
本文介绍了如何将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 < - 这是一个 DATETIMEOFFSET ,但 UTC

CreatedOn: 2008-12-19 06:30:09.0000000 + 00:00 <-- that's a DATETIMEOFFSET, but UTC.

Cheers:)

推荐答案

使用几乎任何样式进行转换都会导致将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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆