How to convert local time to UTC time in SQL
Please help to find how we can convert Local time to UTC time in SQL..?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant SrivastavaRank #581 · 4.2K reputationPosted Feb 5, 2013, 5:27 AM EST
For your First question,
if you change hour to minute or second or any date part, SQL sever puts your Local Time-UTC Difference in that unit. It means, suppose difference is 5 hours between your local time and UTC time and you use 'Seconds'segment here instead of 'Hour', SQL server converts that time difference into seconds (i.e 18000 seconds). Even if you use 'DATE' part, SQL server will give the same UTC time.
For your second question, it takes care your date, month year change it's because you are using Built-in SQL method 'DATEADD()' that is supposed to handle all of these date change issues.
DECLARE @DIFF INT
SET @DIFF = DATEDIFF(HOUR,GETDATE(),GETUTCDATE())
SELECT @DIFF AS DIFFERENCE_IN_HOUR
SELECT DATEADD(HOUR, @DIFF, GETDATE()) AS CURRENT_UTC_TIME
------------------------------------------------------------
DECLARE @DIFF1 INT
SET @DIFF1 = DATEDIFF(MINUTE,GETDATE(),GETUTCDATE())
SELECT @DIFF1 AS DIFFERENCE_IN_MINUTE
SELECT DATEADD(MINUTE, @DIFF1, GETDATE()) AS CURRENT_UTC_TIME
-------------------------------------------------------------
DECLARE @DIFF2 INT
SET @DIFF2 = DATEDIFF(SECOND,GETDATE(),GETUTCDATE())
SELECT @DIFF2 AS DIFFERENCE_IN_SECONDS
SELECT DATEADD(SECOND, @DIFF2, GETDATE()) AS CURRENT_UTC_TIME
------------------------------------------------------------
DECLARE @DIFF3 INT
SET @DIFF3 = DATEDIFF(DAY,GETDATE(),GETUTCDATE())
SELECT @DIFF3 AS DIFFERENCE_IN_DAYS
SELECT DATEADD(DAY, @DIFF3, GETDATE()) AS CURRENT_UTC_TIME
-----------------------------------------------------------
you may look the sql query result:
Hemant SrivastavaRank #581 · 4.2K reputationPosted Feb 4, 2013, 1:59 PM EST
DECLARE @DIFF INT
SET @DIFF = DATEDIFF(HOUR,GETDATE(),GETUTCDATE())
SELECT DATEADD(HH, @DIFF, GETDATE())
Deepesh21 reputationPosted Feb 4, 2013, 2:13 PM EST
First create the function attached in the file. Parameters are local time and your timezone.
Then call it like below:
DECLARE @DT DATETIME = GETDATE()
SELECT @DT AS LOCAL_TIME, dbo.GET_UTCTIME(@DT, 'CT') AS UTC_TIME
Hope this helps and solves your problem.
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 4, 2013, 5:32 PM EST
check below link
it might help inyou problem
http://stackoverflow.com/questions/6064674/sql-server-2008-how-to-convert-gmtutc-datetime-to-local-datetime
Tulika Kumar14 reputationPosted Feb 5, 2013, 4:54 AM EST
Thanks Hemant, you solution is working fine but I have doubts... like:
1. Why are you considering only HOUR part.. what about other minutes and seconds?
2. At that time, when date is changed after UTC conversion, will it work?
Tulika Kumar14 reputationPosted Feb 5, 2013, 8:39 AM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Feb 5, 2013, 1:02 PM EST
Ann Asejo2 reputationPosted Feb 5, 2015, 10:59 PM EST