SQL GETUTCDATE() Function: The Complete Guide
The GETUTCDATE() function calculates this value from the operating system on which the SQL server is running.
SQL GETUTCDATE Function
SQL GETUTCDATE is a built-in function that returns 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. In addition, the 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:
- The operating system settings determine the UTC date and time.
- 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()
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
To get the current database date and time in UTC format in SQL, use the SQL GETUTCDATE() function.