SQL - SYSUTCDATETIME() Function



The SQL SYSUTCDATETIME() function is one of the Date Functions and is useful for returning the most recent system timestamp as a datetime2 value. This SYSUTCDATETIME function's return value is the current Coordinated Universal Time (UTC time). Also, it comes from the computer's operating system (OS), on which the SQL Server instance is running.

The SYSUTCDATETIME() method returns the system time and date in the format yyyy-mm-dd hh:mi:ss.mmmmmmm.we can observe that both SYSUTCDATETIME() and GETUTCDATE() return the same date and time of the computer on which the SQL server is running. But, they only differ in one thing.

  • GETUTCDATE() returns its value as datetime

  • SYSUTCDATETIME() returns its value as a datetime2

Which indicates that SYSUTCDATETIME() provides more seconds precision. The datetime2 data type has a larger range than datetime.

Syntax

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

SYSUTCDATETIME()

Parameters

This function does not accept any parameters.

Example

In the following example, we are trying to retrieving the current UTC date and time −

SELECT GETUTCDATE() AS UTCTimeDate;

Output

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

+-------------------------+
| UTCTimeDate             |
+-------------------------+
| 2023-02-20 05:28:27.850 |
+-------------------------+

Example

In the following example, we are using the convert() function with the sysutcdatetime() function for translating the output into the current UTC date only.

SELECT CONVERT(DATE, SYSUTCDATETIME()) AS UTCDate;

Output

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

+------------+
| UTCDate    |
+------------+
| 2023-02-20 |
+------------+

Example

Let us look into another example, where we are using the CONVERT() function with the sysutcdatetime() function to converting the output into the current time only by using the following query −

SELECT CONVERT(TIME, SYSUTCDATETIME()) AS UTCTime;

Output

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

+------------------+
| UTCTime          |
+------------------+
| 05:34:33.0180845 |
+------------------+

Example

Here, we are using the FORMAT() function to format the date of our requirement by using the following query −

SELECT FORMAT(SYSUTCDATETIME(), 'dd MMMM yyyy, hh:mm tt') AS Result;

Output

When the query gets executed, it will generate an output as shown below −

+----------------------------+
| Result                     |
+----------------------------+
| 20 February 2023, 05:39 AM |
+----------------------------+

Example

In the following example, we are using sysutcdatetime() assigned to a variable of any of the date and time types −

DECLARE @UTC date = SYSUTCDATETIME(); 
SELECT @UTC AS 'UTC Date';

Output

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

+------------+
| UTCDate    |
+------------+
| 2023-02-20 |
+------------+

Example

Look at the following example, where we are going to use the DATEPART() to get only the part of the return value. Execute the below query to retrieve only milliseconds from the sysutcdatetime() −

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

Output

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

+--------------+
| MilliSeconds |
+--------------+
| 861          |
+--------------+

Example

Here, we are using the DATEPART() and retrieving the microseconds in sysutcdatetime() by running the following query −

SELECT DATEPART(microsecond, SYSUTCDATETIME()) AS MicroSeconds;

Output

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

+--------------+
| MicroSeconds |
+--------------+
| 121364       |
+--------------+

Example

In the following example, where we are using the DATENAME() function to display the weekdays name of the current date −

SELECT DATENAME(WEEKDAY, SYSUTCDATETIME()) AS Day;

Output

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

+--------+
| Day    |
+--------+
| Monday |
+--------+

Example

Assume we have created a table with the name Employees in the SQL database using the CREATE statement as shown in the query below −

CREATE TABLE Employees(
   ID INT,
   Name VARCHAR(255),
   LoginStamp DATETIME2,
   PRIMARY KEY (ID)
);

Now, let us insert some records in the EMPLOYEE table using INSERT statements as shown in the query below −

INSERT INTO Employees VALUES (1, 'Shikhar','2019-10-25 09:20:38.4142345');
INSERT INTO Employees VALUES (2, 'Rana','2019-10-25 09:24:35.1231232');
INSERT INTO Employees VALUES (3, 'Ram','2019-10-25 09:25:24.3214589');

We can verify whether the table IPLPlayers is created or not using the following query −

SELECT * FROM Employees;

The table Employees is successfully created in the SQL database −

+----+------------+----------------------------+
| ID | Name       | LoginStamp                 |
+----+------------+----------------------------+
| 1  | Shikhar    | 2019-10-25 09:20:38.4142345|
| 2  | Rana       | 2019-10-25 09:24:35.1231232|
| 3  | Ram        | 2019-10-25 09:25:24.3214589|
+----+------------+----------------------------+

Now we are going to add the row with new employee into the Employees table using sysutcdatetime() by running the below query −

INSERT INTO Employees VALUES (4, 'Suresh', SYSUTCDATETIME());

Verification

To check whether the row has been inserted or not use the following query −

SELECT * FROM Employees;

Output

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

+----+------------+----------------------------+
| ID | Name       | LoginStamp                 |
+----+------------+----------------------------+
| 1  | Shikhar    | 2019-10-25 09:20:38.4142345|
| 2  | Rana       | 2019-10-25 09:24:35.1231232|
| 3  | Ram        | 2019-10-25 09:25:24.3214589|
| 4  | Suresh     | 2023-02-20 06:51:03.2998780|
+----+------------+---------------+------------+
sql-date-functions.htm
Advertisements