GETUTCDATE() Function in SQL Server
GETUTCDATE() :
This function in SQL Server is used to return the UTC date and time of the present database system in a ‘YYYY-MM-DD hh:mm:ss.mmm’ pattern.
Features :
- 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.
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 :
|id_num | message | generated_at ------------------------------------------------------------- 1 | 1 | Its the first message.| 03.01.2021 17:32:16 ------------------------------------------------------------- 2 | 2 | get_utc_date | 03.01.2021 17:32:16
Here, firstly you need to create a table then insert values into it then generate the required output using the GETUTCDATE() function as a default value.
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.
Application :
This function is used to return the current UTC date and time of the database system.
Contact Us