Monday, July 27, 2015

How to convert local time to UTC time in SQL

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:



Resource:
http://hemant-srivastava.blogspot.com/ 

No comments:

Post a Comment