SQL SYSDATETIMEOFFSET() Function Example
SQL SYSDATETIMEOFFSET() is an inbuilt function that returns a value 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 datatype returned is of DATETIMEOFFSET (7) type.
SQL SYSDATETIMEOFFSET()
SQL SYSDATETIMEOFFSET() returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.
Syntax
SELECT SYSDATETIMEOFFSET();
Examples
Query 1
Get the current system date and time with the time zone of the server.
SELECT SYSDATETIMEOFFSET();
Output
2019-11-30 08:56:48.0645108 +07:00
Query 2
Using the DATEPART() function to return the timezone offset.
SELECT SYSDATETIMEOFFSET () AS [System DateTime Offset], DATEPART (TZoffset, SYSDATETIMEOFFSET ()) AS [Timezone Offset];
Output
System DateTime Offset | Timezone Offset |
2019-11-30 08:56:48.0645108 +07:00 | 420 |
Explanation
DATEPART() function returned an integer that represents the time zone offset in minutes.
Query 3
Using FORMAT() function to format the timezone offset.
SELECT SYSDATETIMEOFFSET () AS 'System Date Time Offset', FORMAT (SYSDATETIMEOFFSET (), 'ss') AS 'ss', FORMAT (SYSDATETIMEOFFSET (), 'sss') AS 'sss';
Output
System Date Time Offset | ss | sss |
2019-11-30 08:56:48.0645108 +07:00 | +07 | +07:00 |