使用SQL查询将datetime值从一个时区转换为UTC时区 [英] Convert datetime value from one timezone to UTC timezone using sql query

查看:613
本文介绍了使用SQL查询将datetime值从一个时区转换为UTC时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datetime值。
该日期时间值可以在任何时区中,例如东部标准时间或印度标准时间。
我想在SQL中将该日期时间值转换为UTC时区。
这里的时区值将是给定的参数。
我也可以使用C#代码来实现。

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 timezone in SQL. Here from timezone value will be the given parameter. I can achieve this using C# code also. But I need this in SQL query.

有人可以告诉我如何转换吗?

Can anyone tell me how can I convert that?

推荐答案

时区和时区偏移量是两件事。如果使用夏令时,时区可以具有不同的偏移量。 SQL Server的最新版本是2016年,新增了对时区的支持。

Timezone and timezone offset are two different things. A timezone can have different offsets if daylight savings time is used. Timezone support was added to SQL Server in the latest version, 2016.

您的问题分为两个部分-如何转换 datetime 值转换为具有偏移量/时区的值,然后如何将该值转换为UTC。

Your question has two parts - how to convert a datetime value to a value with offset/timezone and then how to convert that value to a UTC.

在SQL Server 2014之前的版本中,您必须确定提前为您的本地时区设置正确的偏移量,例如使用C#代码。有了它后,您可以将 datetime 转换为具有特定偏移量的`datetimeoffset,并使用 TODATETIMEOFFSET

In versions up to SQL Server 2014, you have to determine the correct offset for your local timezone in advance, eg using C# code. Once you have it you can convert a datetime to a `datetimeoffset with a specific offset with TODATETIMEOFFSET:

select TODATETIMEOFFSET(GETDATE(),'02:00')

select TODATETIMEOFFSET(GETDATE(),120)

这将返回带有原始时间和指定偏移量的 datetimeoffset 值。

This will return a datetimeoffset value with the original time and the specified offset.

执行切换到另一个偏移量(例如UTC)的操作通过 SWITCHOFFSET 函数

Switch to another offset (eg UTC) is performed by the SWITCHOFFSET function

select SWITCHOFFSET(@someDateTimeOffset,0)

您可以将它们与

select SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(),120),0)

偏移量可以作为参数传递。假设您的字段名为 SomeTime ,则可以编写

The offset can be passed as a parameter. Assuming your field is called SomeTime, you could write

select SWITCHOFFSET(TODATETIMEOFFSET(SomeTime,@offsetInMinutes),0)

在SQL Server 2016中,您可以使用时区名称。不过,您仍然需要进行两次转换,首先转换为本地时区,然后转换为UTC:

In SQL Server 2016 you can use the timezone names. You still need a double conversion though, first to the local timezone then to UTC:

SELECT (getdate() at time zone 'Central Europe Standard Time') AT TIME ZONE 'UTC'

第一个 AT TIMEZONE 返回一个 datetimeoffset ,其偏移量为 +2:00 ,第二个将其转换为

The first AT TIMEZONE returns a datetimeoffset with a +2:00 offset and the second converts it to UTC.

注意

如果您使用了 datetimeinfo 类型而不是 datetime 类型。 SQL Server允许对不同偏移量的值进行比较,过滤,计算等,因此您无需进行任何转换即可进行查询。在客户端,.NET具有等效的 DateTimeOffset 类型,因此您无需在客户端代码中进行任何转换。

You could probably avoid all conversions if you used the datetimeinfo type instead of datetime. SQL Server allows comparisons, filtering, calculations etc on values of different offsets, so you wouldn't need to make any conversions for querying. On the client side, .NET has the equivalent DateTimeOffset type so you wouldn't need to make any conversion in client code.

这篇关于使用SQL查询将datetime值从一个时区转换为UTC时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆