Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting offset of datetimeoffset in SQL Server

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!

like image 855
user2105237 Avatar asked Feb 24 '13 19:02

user2105237


People also ask

How do I find the offset in SQL Server?

Extracting the Time Zone Offset You can use the DATEPART() function to return the time zone offset. This function returns an integer that represents the time zone offset in minutes. You can also use the FORMAT() function to return the time zone offset as a string.

How do I get the DateTime offset in SQL?

SQL Server obtains the date and time values by using the GetSystemTimeAsFileTime() Windows API. The accuracy depends on the computer hardware and version of Windows on which the instance of SQL Server is running. The precision of this API is fixed at 100 nanoseconds.

What does DateTimeOffset mean in SQL?

The SQL Server DateTimeOffset data type stores the date & time along with the Time Zone Offset. It is similar to both DateTime & DateTime2 data types. Except that the DateTime & DateTime2 does not store the Time Zone Offset.


1 Answers

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

datepart(tz,@datetimeoffset) 
like image 144
Kenneth Fisher Avatar answered Sep 25 '22 12:09

Kenneth Fisher