将 SQL Server 时区对象 `yyyy-mm-dd HH:MM;SS+HH` 转换为有效的日期时间 (UTC)
我有一个日期时间值的字符串表示形式,其偏移量为 +03
我假设这意味着 +03 小时,我很可能是错误的。
这是我的表中的典型值:
2015-05-15 21:35:19+03
只有两个唯一值 +3
或 +2< /code> 我不确定源系统是什么。
我想知道转换这样的对象的正确方法是什么?我当前的方法是分割偏移量并将剩余部分转换为日期时间对象并使用 dateadd 添加偏移量
SELECT DATEADD(hour, CAST(value AS int)
, CAST(REPLACE(dt, '+' + value, '') AS DATETIME2)) AS offset_val
, dt
FROM (
VALUES ('2015-05-15 21:35:19+03')
, ('2015-05-15 19:35:19+03')
) t(dt)
CROSS APPLY (
SELECT VALUE
, ROW_NUMBER() OVER (ORDER BY VALUE) AS seq
FROM STRING_SPLIT(t.dt, '+')
) ch
WHERE
seq = 1
+---------------------------+----------------------+
|offset_val |dt |
+---------------------------+----------------------+
|2015-05-16 00:35:19.0000000|2015-05-15 21:35:19+03|
|2015-05-15 22:35:19.0000000|2015-05-15 19:35:19+03|
+---------------------------+----------------------+
I have a string representation of datetime values which has an offset +03
I'm assuming this means +03 hours I most likely may be wrong.
This is a typical value in my table:
2015-05-15 21:35:19+03
there are only two unique values +3
or +2
I'm not sure what the source system is/was.
I wonder what the correct way to convert such an object is? my current method is to split the offset and convert the remainder to a datetime object and add the offset using dateadd
SELECT DATEADD(hour, CAST(value AS int)
, CAST(REPLACE(dt, '+' + value, '') AS DATETIME2)) AS offset_val
, dt
FROM (
VALUES ('2015-05-15 21:35:19+03')
, ('2015-05-15 19:35:19+03')
) t(dt)
CROSS APPLY (
SELECT VALUE
, ROW_NUMBER() OVER (ORDER BY VALUE) AS seq
FROM STRING_SPLIT(t.dt, '+')
) ch
WHERE
seq = 1
+---------------------------+----------------------+
|offset_val |dt |
+---------------------------+----------------------+
|2015-05-16 00:35:19.0000000|2015-05-15 21:35:19+03|
|2015-05-15 22:35:19.0000000|2015-05-15 19:35:19+03|
+---------------------------+----------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您以相反的方式得到了偏移量,21:35:19+03 是 UTC+3,它代表 UTC 中的 18:35:19。
话虽如此,您的字符串表示形式可以很好地解析为
datetimeoffset
,然后您可以将其转换为 UTC:You got the offset the other way around, 21:35:19+03 is UTC+3 and it represents 18:35:19 in UTC.
That being said, your string representation parses just fine as
datetimeoffset
and you can then just convert it to UTC: