Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中转换不同的时区_Sql_Sql Server_Sql Server 2008_Datetime_Timezone - Fatal编程技术网

在SQL Server中转换不同的时区

在SQL Server中转换不同的时区,sql,sql-server,sql-server-2008,datetime,timezone,Sql,Sql Server,Sql Server 2008,Datetime,Timezone,我想在SQLServer2008中将东部时间(“GMT-05:00”)转换为IST(“GMT+05:30”) 它应该基于偏差和DayLightBias 例如:东部时间的偏差值为300,DaylightBias值为-60,IST的偏差值为-330,DaylightBias值为-60 我知道如何在C中转换,但我想创建一个作业,为此我需要在SQL Server中进行此转换。在SQL Server 2008或更新版本中使用DATETIMEOFFSET数据类型和SWITCHOFFSET方法: -- def

我想在SQLServer2008中将东部时间(“GMT-05:00”)转换为IST(“GMT+05:30”)

它应该基于偏差和DayLightBias

例如:东部时间的偏差值为300,DaylightBias值为-60,IST的偏差值为-330,DaylightBias值为-60


我知道如何在C中转换,但我想创建一个作业,为此我需要在SQL Server中进行此转换。

在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

  • 除了基偏移和DST偏置外,还有很多事情要考虑。具体来说,不同的偏移在不同日期和不同时间在标准时间和夏令时之间切换

    正确的方法是使用命名时区。与许多其他数据库不同,SQL Server不具有对时区的本机支持。它只支持时区偏移。请参见中的“时区!=偏移”

    幸运的是,我为你做了所有的艰苦工作。使用“我的项目”,您可以编写以下查询:

    SELECT Tzdb.ConvertZone(yourDateTimeValue, 'America/New_York', 'Asia/Kolkata', 1, 1)
    
    时区是标准的,最后的数字选项在项目自述中进行了解释