在 SQL Server 中获取 datetimeoffset 的偏移量 [英] Getting offset of datetimeoffset in SQL Server
问题描述
在 SQL Server 中,我需要找到给定 datetimeoffset(7)
的偏移量.
In SQL Server I need to find the offset of a given datetimeoffset(7)
.
我研究了文档,有很多方法可以更改偏移量,但没有办法知道特定值的偏移量(抱歉,如果我错过了).
I have researched the documentation and there are all ways to change the offset but not a way to know the offset of a particular value (sorry if I missed it).
好吧,我想出了以下一段代码,虽然看起来很有效,但我觉得它太复杂了.
Well I came up with the following piece of code that I find too complicated although seems to work.
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 格式,但想检查是否有更好的方法.
I still have to do the conversion to the +00:00 format but wanted to check if there is a better way of doing this.
谢谢!
推荐答案
datepart 函数有一个 tz 选项,它是以分钟为单位的时区偏移量.
The datepart function has a tz option which is the timezone offset in minutes.
datepart(tz,@datetimeoffset)
这篇关于在 SQL Server 中获取 datetimeoffset 的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!