AppDividend
Latest Code Tutorials

SQL GETUTCDATE Function Example

0

SQL GETUTCDATE function is used for returning the current database date and time in UTC format, i.e. in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format. The GETUTCDATE() function returns the current UTC. The GETUTCDATE() function calculates this value from the operating system on which the SQL server is running.

SQL GETUTCDATE Function

SQL GETUTCDATE() function returns the current database system UTC date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format. 

Syntax

SELECT GETUTCDATE();

Parameters

GETUTCDATE: This function will only return the current database date and time.

NOTE:
  1. The operating system settings determine the UTC date and time.
  2. GETUTCDATE() can also be used to store the timestamp that is independent of Time Zones.

Example

SELECT GETUTCDATE();

Output

2019-11-10 06:51:26.703

For a better understanding, see this example.

See the following code.

DECLARE @local_time DATETIME;
DECLARE @gmt_time DATETIME;
SET @local_time = GETDATE();
SET @gmt_time = GETUTCDATE();
SELECT 'Server local time: '
   + CONVERT(VARCHAR(40),@local_time);
SELECT 'Server GMT time: '
   + CONVERT(VARCHAR(40),@gmt_time);
SELECT 'Server time zone: '
   + CONVERT(VARCHAR(40),
      DATEDIFF(hour,@gmt_time,@local_time));
GO

Output

Server local time: Nov 10 2019 10:06PM
Server GMT time: Nov 10 2019 4:21PM
Server time zone: 6

DIFFERENCE between getdate() and getutcdate():

The main Difference between getdate() and getutcdate() is the time zone number of the SQL SERVER MACHINE.

SQL Server GETUTCDATE() example

The following example uses the GETDATE(), GETUTCDATE() and DATEDIFF() functions to return the local time, UTC, and server time zone.

DECLARE 
    @local_time DATETIME,
    @utc_time DATETIME;

SET @local_time = GETDATE();
SET @utc_time = GETUTCDATE();

SELECT
  CONVERT(VARCHAR(40), @local_time) 
        AS 'Server local time';
SELECT
  CONVERT(VARCHAR(40), @utc_time) 
        AS 'Server UTC time'
SELECT
  CONVERT(VARCHAR(40), DATEDIFF(hour, @utc_time, @local_time)) 
        AS 'Server time zone';
GO

Conclusion

If you want to get the current database date and time in UTC format, then you can use the SQL GETUTCDATE() function.

Related Posts

SQL GETDATE Function

SQL SYSDATETIME Function

SQL Format Function

SQL Date Functions

SQL Datatypes Example

Leave A Reply

Your email address will not be published.

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