Open In App

SQL Query to Convert Date Field to UTC

Improve
Improve
Like Article
Like
Save
Share
Report

In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. In this article, we will discuss how to convert Date Field to UTC in SQL. For this article, we will be using the Microsoft SQL Server as our database.

 

Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks

Query:

 CREATE DATABASE GeeksForGeeks

Output:

Step 2: Use the GeeksForGeeks database. For this use the below command

 Query:

 USE GeeksForGeeks

Output:

Step 3: Create a demo table inside the database GeeksForGeeks

 Query:

 CREATE TABLE Demofordatetime(
 demonumber int);

Output:

Step 4: Write a command to display the current date and time in the default format using the GETDATE() command. We will later convert this to the UTC time i.e. Coordinated Universal Time. 

Query:

 SELECT GETDATE() AS CurrentTime

 CurrentTime is the variable here.

Output:

Step 5: Write a similar command to display the current date and time in the UTC field using the GETUTCDATE() command.

Query:

 SELECT GETUTCDATE() AS UTCTime

  UTCTime is the variable here.

Output:

Step 6: Now write and run both commands simultaneously to see the value of the current date and time both according to the local time zone as well as according to UTC. 

Note – The time difference between IST(Indian Standard Time) and UTC(Coordinated Universal Time) is as follows:

IST – UTC = 5:30 hours i.e. IST is ahead of UTC by 5 hours and 30 minutes.

Query:

 SELECT GETDATE() AS CurrentTime
 SELECT GETUTCDATE() AS UTCTime

Output:


Last Updated : 25 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads