Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 Server_Date_Timestamp With Timezone - Fatal编程技术网

Sql server 从时区的时间戳获取正确的日期

Sql server 从时区的时间戳获取正确的日期,sql-server,date,timestamp-with-timezone,Sql Server,Date,Timestamp With Timezone,我有一个(Azure)SQL Server数据库,时间戳为UTC。我想知道特定时区的日期 下面显示了这个问题 DECLARE @TS DateTime2='2020-02-08 23:00:00' SELECT CAST((@TS at time zone 'W. Europe Standard Time') as date) as StartDate, @TS as StartTimeStampUTC, @TS at time zone 'W. Europe Standard Time' a

我有一个(Azure)SQL Server数据库,时间戳为UTC。我想知道特定时区的日期

下面显示了这个问题

DECLARE @TS DateTime2='2020-02-08 23:00:00'

SELECT CAST((@TS at time zone 'W. Europe Standard Time') as date) as StartDate, @TS as StartTimeStampUTC, @TS at time zone 'W. Europe Standard Time' as StartTimeStampLocalTime
开始UTC:2020-02-08 23:00:00.0000000
当地时间:2020-02-08 23:00:00.0000000+01:00
起始日期:2020-02-08

我预计起始日期值为2020-02-09,因为当地时间为2020-02-08 23:00+01:00=2020-02-09 00:00


我怎样才能得到正确的日期呢?

这似乎可以做到:

SELECT CAST(dateadd(hour,DATEdiff(hour,@TS AT TIME ZONE 'W. Europe Standard Time',@TS ),@TS) as date) as StartDate

答案现在是2020-02-09,正如预期的那样

您可以在SQL Server中遵循下面提到的过程。 1.使用-GETUTCDATE()获取UTC时间 2.使用以下链接获取日期时间和UTC日期时间之间的差异- 3.添加UTC日期时间的差异

请找到下面的脚本- 选择GETDATE()作为“本地时区”, GETUTCDATE()作为“Utc\u时区”, (DATEADD(小时,5,DATEADD(分钟,30,GETUTCDATE()))作为“基于UTC的本地时区”

根据我当地的时区,我增加了5:30小时


使用两个连续的at时区:第一个用于设置utc的日期时间(因为您存储在utc),第二个用于计算eu的日期时间。st.然后从最终结果中获取日期:选择cast(cast('2020-02-08 23:00:00.0000000'作为日期时间2)在时区“UTC”在时区“W.欧洲标准时间”作为日期)