在SQL Server中获取datetimeoffset的偏移量

时间:2013-02-24 19:57:28

标签: sql sql-server sql-server-2008 tsql sql-server-2012

在SQL Server中,我需要找到给定datetimeoffset(7)的偏移量。

我已经研究了文档,并且有各种方法可以更改偏移量但不知道某个特定值的偏移量(抱歉,如果我错过了它)。

我想出了下面的一段代码,虽然看起来很复杂,但我发现它太复杂了。

DECLARE @datetimeOffset datetimeoffset(7)

SET @datetimeOffset = CAST('2007-05-08 22:35:29.1234567-05:00' AS datetimeoffset(7))

DECLARE @result datetimeoffset(7)
DECLARE @offsetMin int

SET @offsetMin = DATEDIFF(mi, cast(SWITCHOFFSET(@datetimeOffset, '+00:00') as datetime), cast(@datetimeOffset as datetime)) 

SELECT @offsetMin

我仍然需要转换为+00:00格式,但想检查是否有更好的方法。

谢谢!

1 个答案:

答案 0 :(得分:36)

datepart函数有一个tz选项,它是以分钟为单位的时区偏移量。

datepart(tz,@datetimeoffset)