SQL Query to Convert UTC to Local Time Zone
Last Updated :
13 Dec, 2022
In this article, we will cover how to convert UTC to the local time zone for the different times of UTC in the database. Now, let’s execute a query that converts the UTC to local time zone using MSSQL as our database in detail step-by-step.
Step 1: Creating a database time_converter by using the following SQL query as follows.
CREATE DATABASE time_converter;
Step 2: Using the database time_converter using the following SQL query as follows.
USE time_converter;
Step3: Creating table times with 2 columns using the following SQL query as follows.
Let us create a table with index and DATETIME as a datatype.
CREATE TABLE times
(Sno INT,
date_time DATETIME);

Step 4: To view the description of the tables in the database using the following SQL query as follows.
EXEC sp_columns times;

Step 5: Inserting rows into times table using the following SQL query as follows.
INSERT INTO times VALUES
(1,'2021-03-01 12:00:00'),
(2,'2021-04-01 09:30:00'),
(3,'2021-02-05 10:50:00'),
(4,'2021-04-18 08:50:00');

Step 6: Viewing the table times after inserting rows by using the following SQL query as follows.
SELECT * FROM times;

Query 1: Query to convert current time from UTC to local IST (Indian Standard Time)
Note: India’s time zone is 5hours and 30 minutes ahead of UTC.
Syntax:
SELECT column_name,
CONVERT(datetime,
SWITCHOFFSET(CONVERT(DATETIMEOFFSET, column_name),
DATENAME(TZOFFSET, SYSDATETIMEOFFSET())))
AS general_name
SELECT
CONVERT(datetime,
SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()),
DATENAME(TZOFFSET, SYSDATETIMEOFFSET())))
AS LOCAL_IST;
Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.

Query 2: Query to convert all the times in times table to local IST from UTC.
Method 1:
In this method, the UTC times in the table get converted to local Indian standard time. To get the UTC converted to IST, UTC must be added with “+05:30” the same gets added in the output.
Syntax:
SELECT column_name,
CONVERT(datetime,
SWITCHOFFSET(CONVERT(DATETIMEOFFSET, column_name),
DATENAME(TZOFFSET, SYSDATETIMEOFFSET())))
AS general_name
FROM table_name;
SELECT date_time,
CONVERT(datetime,
SWITCHOFFSET(CONVERT(DATETIMEOFFSET, date_time),
DATENAME(TZOFFSET, SYSDATETIMEOFFSET())))
AS LOCAL_IST
FROM times;

Method 2:
In this method, the difference between the present date-time(GETDATE()) and UTC (GETUTCDATE()) date time gets added to the dates in date_time column.
Syntax:
SELECT
column_name,
DATEADD(MI, DATEDIFF(MI, GETUTCDATE(), GETDATE()), date_time)
AS LOCAL_IST
FROM times;
SELECT date_time,
DATEADD(MI, DATEDIFF(MI, GETUTCDATE(), GETDATE()), date_time)
AS LOCAL_IST
FROM times;

Similar Reads
SQL Query to Convert Date Field to UTC
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
2 min read
SQL Query to Convert Datetime to String
In order to convert a DateTime to a string, we can use CONVERT() and CAST() function. These functions are used to converts a value(of any datatype) into a specified datatype. CONVERT() Function Syntax: CONVERT(VARCHAR, datetime [,style])VARCHAR - It represent the string type.datetime - It can be the
3 min read
How to Convert from UTC to Local Time in Moment JS?
Moment.js is a powerful library for working with dates and times in JavaScript. One common requirement is to convert a date and time from Coordinated Universal Time (UTC) to the local time zone of the user's device. This is especially useful for applications that need to display times in the user's
2 min read
SQL Query to Convert Datetime to Epoch
Converting a datetime value to Epoch time is a common operation in SQL, particularly when working with timestamps in various applications. In this article, We will learn a step-by-step process of creating a SQL database, inserting datetime values and converting those values into Epoch time using SQL
4 min read
Convert Datetime Object To Local Time Zone In Pandas
Pandas has established itself as one of the most powerful and versatile libraries in Python. When working with time series data, handling datetime objects efficiently becomes paramount. Oftentimes, datasets contain timestamps in various time zones, necessitating conversion to a consistent reference
2 min read
How to Convert Epoch Time to Date in SQL?
To convert Epoch Time to Date in SQL use the DATEADD() function. DATEADD() function in SQL Server is used to add a time or date interval to a specified date and return the modified date. Some features of the SQL DATEADD() function are: This function is used to sum up a time or a date interval to a d
2 min read
SQL Query to Convert Datetime to Date
In SQL Server, working with DateTime data types can be a bit complex for beginners. This is because DateTime includes both the date and time components, while many scenarios only require the date. Whether you're working with large datasets, performing data analysis, or generating reports, it's commo
4 min read
SQL Query to Convert Date to Datetime
In this article, we will look at how to convert Date to Datetime. We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type.Conve
1 min read
How to convert Moment.js date to users local timezone?
Moment.js is a JavaScript library used for parsing, validating, manipulating, and displaying dates and times. When we are working with dates, sometimes we need to display the time in the user's local timezone. For example, when we are building a global application where users are in different parts
2 min read
How to Convert Timestamp to Datetime in MySQL?
In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
2 min read