Getting offset of datetimeoffset in SQL Server

一个人想着一个人 提交于 2020-01-31 22:19:18

问题


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

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.

Thanks!


回答1:


The datepart function has a tz option which is the timezone offset in minutes.

datepart(tz,@datetimeoffset)


来源:https://stackoverflow.com/questions/15055963/getting-offset-of-datetimeoffset-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!