SQL - SYSDATETIMEOFFSET() Function



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

The SYSDATETIMEOFFSET function returns a datetime2 data type with the format yyyy-mm-dd hh:mm:ss.nnnnnn.

Syntax

Following is the syntax of the SQL SYSDATETIMEOFFSET() function −

SYSDATETIMEOFFSET()

Parameters

This function does not accept any parameters.

Example

Let us retrieve the sysdatetimeoffset by using the following query −

SELECT SYSDATETIMEOFFSET() AS Result;

Output

When we execute the above query, the output is obtained as follows −

+------------------------------------+
| Result                             |
+------------------------------------+
| 2023-02-17 16:23:11.4906580 +05:30 |
+------------------------------------+

Example

In the following example, we are going to use DATEPART() function to retrieve the offset of time zone, which returns an integer that represents the offset of time zone in minutes by running the following query −

SELECT SYSDATETIMEOFFSET() AS 'Date and Time',
DATEPART(TZoffset, SYSDATETIMEOFFSET()) AS 'TimeZone Offset';

Output

On executing the above query, the output is displayed as follows −

+------------------------------------+-----------------+
| Date and Time                      | TimeZone Offset |
+------------------------------------+-----------------+
| 2023-02-17 16:30:19.6822375 +05:30 | 330             |
+------------------------------------+-----------------+

Example

Here, we are using FORMAT() function to retrieve the offset of time zone in a string specifically, we can also use the zz, z, and zzz arguments to get the return value in required format.

Let us look into the example, where we are using the FORMAT() function to return the offset of time zone by using the following query −

SELECT SYSDATETIMEOFFSET() AS 'Date and Time',
   FORMAT(SYSDATETIMEOFFSET(), 'zz') AS 'zz';

Output

The output for the above query is produced as given below −

+------------------------------------+-----+
| Date and Time                      | zz  |
+------------------------------------------+
| 2023-02-17 16:30:19.6822375 +05:30 | +05 |
+------------------------------------+-----+

Example

Here we are using the CONVERT() function for converting the return value into other data types.

Look at the following example, where we are going to use the CONVERT() function and converting the return value into date values by using the following query −

SELECT CONVERT (DATE, SYSDATETIMEOFFSET()) AS 'Date';

Output

If we execute the above query, the result is produced as follows −

+------------+
| Date       |
+------------+
| 2023-02-17 |
+------------+

Example

In this example, we are using the DATEDIFF() function along with the SYSDATETIMEOFFSET() function to find the differences in minutes and seconds between the current datetime and variable −

DECLARE @DATETIME DATETIME2
SET @DATETIME = '2023-02-17 05:40:10.3021234'
SELECT 'Minutes_Difference' AS 'MINUTE', DATEDIFF(MINUTE, @DATETIME, SYSDATETIMEOFFSET()) AS NumberofMinutes;
SELECT 'Seconds_Difference' AS 'SECOND', DATEDIFF(SECOND, @DATETIME, SYSDATETIMEOFFSET()) AS NumberofSeconds;

Output

On executing the above query, it will generate the following output as shown below −

+--------------------+-----------------+
| MINUTE             | NumberofMinutes |
+--------------------+-----------------+
| Minutes_Difference | 391             |
+--------------------+-----------------+

+--------------------+-----------------+
| SECOND             | NumberofSeconds |
+--------------------+-----------------+
| Seconds_Difference | 23460           |
+--------------------+-----------------+

Example

In the following example, we are going to use DATENAME() to retrieve the month name from today´s date and time by running the following query −

SELECT DATENAME(month, SYSDATETIMEOFFSET()) AS PresentMonth; 

Output

On running the above query, it will generate the output as shown below −

+--------------+
| PresentMonth |
+--------------+
| February     |
+--------------+

Example

In the following example, we are going to use the DATEPART() function to retrieve the milliseconds based on the present date and time by running the following query −

SELECT DATEPART(millisecond, SYSDATETIMEOFFSET()) AS MilliSeconds;

Output

On executing the above query, it will generate the following output as shown below −

+--------------+
| MilliSeconds |
+--------------+
| 718          |
+--------------+

Example

Here, we are using the CONVERT() function and converting the return value into a time value by using the following query −

SELECT CONVERT (TIME, SYSDATETIMEOFFSET()) AS 'Time';

Output

On running the above query, it will generate the output as shown below −

+------------------+
| Time             |
+------------------+
| 18:00:20.2808586 |
+------------------+
sql-date-functions.htm
Advertisements