- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
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| +----+------------+---------------+------------+
To Continue Learning Please Login
Login with Google