SQL GETUTCDATE

The SQL GETUTCDATE() function returns the current date and time in Coordinated Universal Time (UTC). This function is useful when you need to store or compare timestamps across different time zones to maintain consistency in applications.

In this tutorial, we will explore the GETUTCDATE() function, its syntax, and practical use cases with examples.


Syntax of SQL GETUTCDATE Function

The GETUTCDATE() function does not require any parameters. Its basic syntax is:

</>
Copy
SELECT GETUTCDATE();

Explanation:

  • Returns the current date and time in UTC format.
  • The output format is YYYY-MM-DD hh:mm:ss.mmm (e.g., 2025-02-12 14:30:15.123).
  • It does not consider the server’s local time zone.

Step-by-Step Examples Using GETUTCDATE

1. Retrieving the Current UTC Date and Time

To fetch the current UTC date and time, use the following query:

</>
Copy
SELECT GETUTCDATE() AS current_utc_time;

Example Output:

Explanation:

  • This query retrieves the current UTC time from the SQL Server.
  • It is useful when you want to log timestamps independent of server location.

2. Storing UTC Timestamps in a Table

Let’s create an employees table to store employee login timestamps in UTC.

</>
Copy
CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    login_time DATETIME DEFAULT GETUTCDATE()
);

Now, insert some records:

</>
Copy
INSERT INTO employees (name) VALUES 
('Arjun'),
('Ram'),
('Priya');

Retrieve stored data:

</>
Copy
SELECT * FROM employees;

Example Output:

Explanation:

  • The login_time column is automatically set to the current UTC time when an employee logs in.
  • This ensures timestamps are consistent across different time zones.

Conclusion

The GETUTCDATE() function is used for handling date-time values in UTC format. In this tutorial, we covered:

  1. The syntax and usage of GETUTCDATE().
  2. Retrieving the current UTC timestamp.
  3. Storing UTC timestamps in a database table.