Convert datetime value from one timezone to UTC timezone using sql query

前端 未结 4 1228
北海茫月
北海茫月 2020-12-31 05:36

I have a datetime value. That datetime value may be in any timezone like \'Eastern Standard Time\' or \'India Standard Time\'. I want to convert that datetime value to UTC t

相关标签:
4条回答
  • 2020-12-31 05:36

    To convert to a time zone using the time zone name, you can do the following:

    Find the number of minutes between the server timezone and your time zone
    -- Change to your time zone name
    declare @TimezoneName varchar(max) = 'New Zealand Standard Time'
    declare @timezoneOffset bigint = Datediff( MINUTE, getdate() at time zone @TimezoneName, getdate() )
    

    Use SWITCHOFFSET to give you the datetime in your timezone.

    eg when getdate() is '2020-04-16 04:47:25.640' you get '2020-04-16 16:47:25.640 +12:00' (+12 for NZT)

    select SWITCHOFFSET(getdate(), @timezoneOffset) 
    

    This should work in SQL Server 2016 and above.

    0 讨论(0)
  • 2020-12-31 05:43

    Timezone and timezone offset are two different things. A timezone can have different offsets if daylight savings time is used. Timezone support was added to SQL Server in the latest version, 2016.

    Your question has two parts - how to convert a datetime value to a value with offset/timezone and then how to convert that value to a UTC.

    In versions up to SQL Server 2014, you have to determine the correct offset for your local timezone in advance, eg using C# code. Once you have it you can convert a datetime to a `datetimeoffset with a specific offset with TODATETIMEOFFSET:

    select TODATETIMEOFFSET(GETDATE(),'02:00')
    

    or

    select TODATETIMEOFFSET(GETDATE(),120)
    

    This will return a datetimeoffset value with the original time and the specified offset.

    Switch to another offset (eg UTC) is performed by the SWITCHOFFSET function

    select SWITCHOFFSET(@someDateTimeOffset,0)
    

    You can combine both with

    select SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(),120),0)
    

    The offset can be passed as a parameter. Assuming your field is called SomeTime, you could write

    select SWITCHOFFSET(TODATETIMEOFFSET(SomeTime,@offsetInMinutes),0)
    

    In SQL Server 2016 you can use the timezone names. You still need a double conversion though, first to the local timezone then to UTC:

    SELECT (getdate() at time zone 'Central Europe Standard Time') AT TIME ZONE 'UTC'
    

    The first AT TIMEZONE returns a datetimeoffset with a +2:00 offset and the second converts it to UTC.

    NOTE

    You could probably avoid all conversions if you used the datetimeinfo type instead of datetime. SQL Server allows comparisons, filtering, calculations etc on values of different offsets, so you wouldn't need to make any conversions for querying. On the client side, .NET has the equivalent DateTimeOffset type so you wouldn't need to make any conversion in client code.

    0 讨论(0)
  • 2020-12-31 05:50

    If you are using SQL Server 2016 you can use the new AT TIME ZONE clause:

    SELECT SalesOrderID, OrderDate,   
        OrderDate AT TIME ZONE 'Eastern Standard Time' AS OrderDate_TimeZoneEST,  
        OrderDate AT TIME ZONE 'Eastern Standard Time'   
        AT TIME ZONE 'UTC' AS OrderDate_TimeZoneUTC  
    FROM Sales.SalesOrderHeader;  
    
    0 讨论(0)
  • 2020-12-31 05:51

    for older then sql server 2012 use below query, it worked for me.

    DECLARE @localdate DATETIME = '2020-01-10 05:30';
    Declare @DateDiff bigint =Datediff(minute, Getdate(),Getutcdate() ) --GetDate -current datetime of local
    --Declare @DateDiff bigint =-330  --you ca declare timezone different in minutes also
    SELECT @localdate AS LocalDate, Dateadd(minute, @DateDiff, @localdate) AS UTCConvertedDate;
    
    0 讨论(0)
提交回复
热议问题