SQL Server SYSDATETIMEOFFSET Function

Summary: in this tutorial, you will learn how to use the SQL Server SYSDATETIMEOFFSET() function to get the current system date and time with the time zone.

SQL Server SYSDATETIMEOFFSET() function overview

The SYSDATETIMEOFFSET() function returns a value of DATETIMEOFFSET(7) that represents the current system date and time, which also includes the time zone, of the computer on which the SQL Server instance is running.

The following illustrates the syntax of the SYSDATETIMEOFFSET() function:

SYSDATETIMEOFFSET()
Code language: SQL (Structured Query Language) (sql)

For example, to get the current system date and time with the timezone of the Server to which you are connecting, you use this statement:

SELECT 
    SYSDATETIMEOFFSET() [datetimeoffset with timezone];
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

datetimeoffset with timezone
----------------------------------
2019-05-03 08:56:48.0645108 +07:00

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

SQL Server SYSDATETIMEOFFSET() function examples

Let’s take some examples of using the SYSDATETIMEOFFSET() function.

A) Extracting the time zone offset example

The following example uses the DATEPART() function to return the time zone offset. It returns an integer that represents the time zone offset in minutes.

SELECT 
    SYSDATETIMEOFFSET() AS [System DateTime Offset],
    DATEPART(TZoffset, SYSDATETIMEOFFSET()) AS [Timezone Offset];
Code language: SQL (Structured Query Language) (sql)

Here is the output:

System DateTime Offset             Timezone Offset
---------------------------------- ---------------
2019-05-03 09:07:30.4093274 +07:00 420

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

B) Formatting the Time Zone Offset example

You can also use the FORMAT() function to return the time zone offset as a string by using :

This example formats the returned timezone offset as a string by using the FORMAT() function with the zz and zzz arguments:

SELECT 
    SYSDATETIMEOFFSET() AS 'System Date Time Offset', 
    FORMAT(SYSDATETIMEOFFSET(), 'zz') AS 'zz', 
    FORMAT(SYSDATETIMEOFFSET(), 'zzz') AS 'zzz';
Code language: SQL (Structured Query Language) (sql)

The following picture shows the output:

SQL Server SYSDATETIMEOFFSET

In this tutorial, you have learned how to use the SQL Server SYSDATETIMEOFFSET() function to get the current system date and time with a time zone.

Was this tutorial helpful?