GETUTCDATE() Function in SQL Server
Last Updated :
23 Sep, 2024
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.
Similar Reads
SQL Server GETDATE() Function
SQL Server GETDATE() function returns the current date and time of the database system in a 'YYYY-MM-DD hh:mm:ss.mmm' format. Note: GETDATE() is similar to other date and time functions like CURRENT_TIMESTAMP, SYSDATETIME(), and GETUTCDATE(), but differs primarily in terms of the time zone and preci
2 min read
SQL Server LEN() Function
SQL SERVER LEN() function calculates the number of characters of an input string, excluding the trailing spaces. LEN() Function in SQL ServerThe LEN function in the SQL Server fetches the number of characters in a string. It counts the preceding spaces but not the trailing spaces. For eg, 'SQL SERVE
2 min read
GROUPING ID Function in SQL Server
SQL Server is a Relational Database Management System that is used to create and manipulate the database. It provides advanced security measures like encryption, access control, and auditing to protect sensitive data from unauthorized access. It Supports a wide range of data types, including structu
6 min read
ISDATE() Function in SQL Server
ISDATE() function : This function in SQL Server is used to check if the specified expression is a valid date or not. Features : This function is used to find if the stated date is valid or not. This function comes under Date Functions. This function accepts only one parameter i.e, an expression. Thi
2 min read
REPLICATE() Function in SQL Server
REPLICATE() function : This function in SQL Server is used to repeat the specified string for a given number of times. Features : This function is used to return the stated string for a given number of times. This function accepts only strings and integers as parameter. This function returns an erro
2 min read
SQL Server ROUND() Function
The SQL Server ROUND() function rounds off a specified number to a decimal place. If the length is negative and larger than the number of digits before the decimal point, ROUND returns 0. ROUND in SQL ServerThe ROUND() function in SQL Server rounds off a number to a specified decimal place. It accep
2 min read
PI() Function in SQL Server
PI() function : This function in SQL Server is used to return the constant float value of math Pi. The default number of decimal places displayed is seven, but SQL Server uses the full double-precision value internally. Features : This function is used to get the value of pi.This function does not a
1 min read
DAY() Function in SQL Server
DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
2 min read
EXP() Function in SQL Server
EXP() function : This function in SQL Server is used to return a value which is e raised to the nth power, where n is a specified number. Here âeâ is a mathematical constant and the base of natural logarithms whose value is 2.7182818 and n is the input number taken as the parameter of the function.
2 min read
SUM() Function in SQL Server
The SUM() function in SQL Server is an essential aggregate function used to calculate the total sum of values in a numeric column. It aggregates data by summing up all values in the specified column for the rows that match the criteria of the query. In this article, We will learn about SUM() Functio
3 min read