Open In App

GETUTCDATE() Function in SQL Server

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The GETUTCDATE() function in SQL Server is a built-in function that retrieves the current date and time in Coordinated Universal Time (UTC). This function is particularly useful for applications that need to operate independently of time zones.

In this article, We will learn about the GETUTCDATE() Function in SQL Server in detail by understanding various examples and so on.

GETUTCDATE() Function in SQL Server

  • The GETUTCDATE() function in SQL Server is a built-in function that returns the current date and time in Coordinated Universal Time (UTC).
  • It is useful for applications that need to operate in a time zone-independent manner or when storing timestamps in a consistent format across different geographical locations.

Features of GETUTCDATE() Function in SQL Server

  • This function is used to find the UTC date and time of the current database system.
  • This function comes under Date Functions.
  • This function doesn’t accept any parameter.
  • This function returns the output in ‘YYYY-MM-DD hh:mm:ss.mmm‘ format.

Syntax:

GETUTCDATE()

Parameter:

This method doesn’t accept any parameter.

Returns:

It returns the UTC date and time of the current database system in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.

Examples of GETUTCDATE() Function in SQL Server

Example 1:

Using the GETUTCDATE() function and getting the output.

SELECT GETUTCDATE();

Output:

2021-01-03 15:34:14.403

Here, the output will vary each time the code is compiled as this method returns the current UTC date and time.

Example 2:

Using GETUTCDATE() as a default value in the below example and getting the output.

CREATE TABLE get_utc_date
(
id_num INT IDENTITY,
message VARCHAR(150) NOT NULL,
generated_at DATETIME NOT NULL
DEFAULT GETUTCDATE(),
PRIMARY KEY(id_num)
);
INSERT INTO get_utc_date(message)
VALUES('Its the first message.');

INSERT INTO get_utc_date(message)
VALUES('get_utc_date');

SELECT
id_num,
message,
generated_at
FROM
get_utc_date;

Output:

Here’s the table with an explanation added:

id_num message generated_at
1 It’s the first message. 03.01.2021 17:32:16
2 get_utc_date 03.01.2021 17:32:16

Explanation:

  • id_num: A unique identifier for each message.
  • message: The content of the message. The first entry indicates a standard message, while the second entry refers to the function get_utc_date, which retrieves the current UTC date and time in SQL Server.
  • generated_at: The timestamp indicating when each message was generated, formatted as DD.MM.YYYY HH:MM

Note: For running the above code use SQL server compiler, you can also use an online compiler.

Example 3:

Using CONVERT() function in order to translate the output of the GETUTCDATE() function into the current date only.

SELECT CONVERT(DATE, GETUTCDATE());

Output :

2021-01-07

Here, the output may vary every time the code is compiled as it returns current date.

Example 4:

Using CONVERT() function in order to translate the output of the GETUTCDATE() function into the current time only.

SELECT CONVERT(TIME, GETUTCDATE());

Output:

06:40:14.4700000

Here, the output may vary every time the code is compiled as it returns current time.

Conclusion

In summary, the GETUTCDATE() function is an essential tool for obtaining the current UTC date and time in SQL Server. Its straightforward syntax and reliable output format make it an ideal choice for applications that require time zone-independent functionality.

What does the GETUTCDATE() function return?

The GETUTCDATE() function returns the current UTC date and time from the server where the database is hosted, formatted as ‘YYYY-MM-DD hh:mm.mmm’.

Can I use GETUTCDATE() to get local time?

No, GETUTCDATE() specifically returns the current time in UTC. To get the local time, you would need to use other functions like GETDATE() or convert the UTC time to your local time zone.

Is there any parameter required for the GETUTCDATE() function?

No, GETUTCDATE() does not accept any parameters. It is a simple function that provides the current UTC date and time without any additional input.



Next Article
Article Tags :

Similar Reads