AppDividend
Latest Code Tutorials

SQL SYSUTCDATETIME Function Example

SQL SYSUTCDATETIME() is an inbuilt Date function that is used to return the Current Date and Time of the system i.e., DATETIME2 on which the SQL Server instance is running. The DATETIME is in coordinated Universal Time (UTC). The format is: ‘yyyy-mm-dd hh:mm:ss.nnnnnnn’.

SQL SYSUTCDATETIME()

SQL SYSUTCDATETIME() returns the datetime2 value that contains a date and time of a computer on which an instance of SQL Server is running. The date and time are returned as UTC time (Coordinated Universal Time). The fractional second precision specification has the range from 1 to 7 digits. The default precision is 7 digits.

Syntax

SELECT SYSUTCDATETIME();

Note

  1. The function is similar to the GETUTCDATE () function having some differences.
  2. SYSUTCDATETIME returns the fractional seconds precision up to 7, whereas the GETUTCDATE returns the fractional seconds precision up to 3.
  3. SYSUTCDATETIME returns datetime2 as the data type, whereas the GETUTCDATE returns DateTime as the data type.
  4. The function is a non-deterministic type; therefore, views and columns that have expressions reference this function cannot be indexed.

Examples

Query 1

SELECT SYSUTCDATETIME () AS CURRENT_DATE;

Output

CURRENT_DATE
2019-11-25 14:55:33.3975855

 

Query 2

Using CONVERT () function to convert SYSUTCDATETIME function to Current Date.

SELECT CONVERT (DATE, SYSUTCDATETIME ());

Output

2019-11-25

Query 3

Using CONVERT () function to convert SYSUTCDATETIME function to Current Time.

SELECT CONVERT (TIME, SYSUTCDATETIME ());

Output

15:08:33.3975855

Query 4

Using the DATEPART() function to display MilliSeconds, Micro Seconds and Nano Seconds.

Queries

SELECT 'Milli Seconds' AS 'MILLISECOND', DATEPART (millisecond, SYSUTCDATETIME ()) AS [ MilliSeconds_Value]; 

SELECT 'Micro Seconds' AS 'MICROSECOND', DATEPART (microsecond, SYSUTCDATETIME ()) AS [MicroSeconds_Value]; 

SELECT 'Nano Seconds' AS 'NANOSECOND', DATEPART (nanosecond, SYSUTCDATETIME ()) AS [NanoSeconds_Value];

Output

MILLISECONDMilliSeconds_Value
Milli Seconds397

 

MICROSECONDMicroSeconds_Value
Micro Seconds397585

 

NANOSECONDSNanoSeconds_Value
Nano Seconds39758500

 

Query 5

Using the DATENAME() function to display Weekday name from Current Date and Time.

SELECT 'Day' AS 'DAY', DATENAME (WEEKDAY, SYSUTCDATETIME ()) AS [Day_Name]; 

Output

DAYDay_Name
DayMonday

Query 6

SELECT 'Tomorrow' AS 'DAY', DATEADD (day, 1, SYSUTCDATETIME ()) AS [Next_Date];

Output

DAYNext_Date
Tomorrow2019-11-26 14:55:33.3975855

See also

SQL Date

SQL GETUTCDATE()

SQL GETDATE()

SQL SYSDATETIME()

SQL CURRENT_TIME()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.