简体   繁体   中英

Convert different time zone in SQL Server

I want to convert Eastern time ("GMT-05:00") into IST ("GMT+05:30") in SQL Server 2008.

It should based on Bias & DayLightBias .

Ex: Eastern Time has Bias value 300 & DaylightBias value -60 & IST has Bias value -330 & DayLightBias value -60.

I know how to convert this in C# but I want to create a job and for that I need this conversion in SQL Server.

Use the DATETIMEOFFSET datatype and the SWITCHOFFSET method in SQL Server 2008 or newer:

-- define your input in Eastern Time
DECLARE @Input DATETIMEOFFSET = SYSDATETIME()
-- SET @Input = SWITCHOFFSET(@Input, '-05:00')
SET @Input = SWITCHOFFSET(@Input, -300)

DECLARE @output DATETIMEOFFSET

-- convert Eastern Time to IST
-- SET @output = SWITCHOFFSET(@input, '+05:30')
SET @output = SWITCHOFFSET(@input, 330)

SELECT @Input, @output

There are a lot more things to consider other than just the base offset and dst bias. Specifically, different offsets switch between standard and daylight time on different dates and at different times.

The correct way is with a named time zone. Unlike many other databases, SQL Server does not have native support for time zones. It only has support for time zone offsets . See "Time Zone != Offset" in the timezone tag wiki .

Fortunately, I've done all the hard work for you. Using my SQL Server Time Zone Support project, you can write this query:

SELECT Tzdb.ConvertZone(yourDateTimeValue, 'America/New_York', 'Asia/Kolkata', 1, 1)

The time zones are standard IANA tz database identifiers , and the numerical options at the end are explained in the project's readme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM