Sql Time Conversion to UTC

时间:2018-06-18 11:42:11

标签: sql-server sql-server-2014

I need to write a SQL Stored Procedure which returns UTC time when we give a date and time along with another time zone.

for e.g. I will give input as @TimeZone='IST and @Time='2018-06-18 17:08:31.383' and I expect the UTC time as output which is '2018-06-18 11:38:31.383'

I already have this statement ,

DATEADD(mi, DATEDIFF(mi, GETDATE(), GETUTCDATE()), @Time)

but this needs to run on different Servers to return different UTC's .Is there any possibility to obtain this in SQL Server 2014.

2 个答案:

答案 0 :(得分:0)

SQL服务器不知道IST是什么。此外,某些时区使用夏令时,这使得这更加复杂(不同日期不同地点的夏令时变化)。您可以使用带有时区和differencefromutc列的表,但这很难实现。

再看一下,如果您使用的是SQL 2017,AT TIME ZONE可能就是您想要的。

答案 1 :(得分:0)

如果要将表示为非UTC的日期时间转换为其UTC值,可以在案例构造中使用dateadd。

CASE 
  WHEN @TimeZone='IST' THEN DATEADD(mi, 330, @time) 
  WHEN @TimeZone='GMT-1' THEN DATEADD(mi, -60, @time) 
......
......
ELSE @time END

确实没有不同的UTC,因为UTC是格林威治时区所指的世界时间,它只有一个。

相关问题