简体   繁体   English

在SQL Server中转换不同的时区

[英]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. 我想在SQL Server 2008中将东部时间(“ GMT-05:00”)转换为IST(“ GMT + 05:30”)。

It should based on Bias & DayLightBias . 它应该基于Bias和DayLightBias

Ex: Eastern Time has Bias value 300 & DaylightBias value -60 & IST has Bias value -330 & DayLightBias value -60. 例如:东部时间的Bias值为300,DaylightBias值为-60,IST的Bias值为-330,DayLightBias值为-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. 我知道如何在C#中进行转换,但是我想创建一个作业,为此我需要在SQL Server中进行转换。

Use the DATETIMEOFFSET datatype and the SWITCHOFFSET method in SQL Server 2008 or newer: 在SQL Server 2008或更高版本中使用DATETIMEOFFSET数据类型和SWITCHOFFSET方法:

-- 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. 除了基本偏移和dst偏差外,还有很多其他要考虑的事情。 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. 与许多其他数据库不同,SQL Server没有对时区的本机支持。 It only has support for time zone offsets . 它仅支持时区偏移量 See "Time Zone != Offset" in the timezone tag wiki . 请参阅时区标签Wiki “时区!=偏移”。

Fortunately, I've done all the hard work for you. 幸运的是,我已经为您完成了所有艰苦的工作。 Using my SQL Server Time Zone Support project, you can write this query: 使用我的SQL Server时区支持项目,可以编写以下查询:

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. 时区是标准的IANA tz数据库标识符 ,最后的数字选项在项目的自述文件中进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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