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

前端 未结 4 1266
北海茫月
北海茫月 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.

提交回复
热议问题