如何从 DateTimeOffset 对象中选择 UTC 偏移量? [英] How to SELECT the UTC offset from a DateTimeOffset object?

查看:31
本文介绍了如何从 DateTimeOffset 对象中选择 UTC 偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用:

SELECT DATEPART(TZ, SYSDATETIMEOFFSET())

但是,它以分钟为单位返回偏移量.我想保留 '-05:00' 而不是 '-300' 的格式

However, it returns the offset in minutes. I would like to preserve the format of '-05:00' instead of '-300'

谢谢.

推荐答案

如果要在 datetimeoffset 变量的末尾提取确切的字符串-05:00",可以使用 SQL Server 字符串操作.我认为使用内置的 SQL DateTime 函数无法做到这一点.您可以使用 CAST 函数,我相信默认为 ISO 8601 格式:

If you want to extract the exact string '-05:00' at the end of a datetimeoffset variable, you can use SQL Server string manipulation. I do not think it is possible to do this using the built-in SQL DateTime functions. You can use the CAST function, which I believe defaults to ISO 8601 format:

declare @timeStr nvarchar(50) = CAST(SYSDATETIMEOFFSET() as nvarchar(50))
select right(@timeStr, 6)

如果想更明确,可以使用格式类型为126的CONVERT函数,明确告诉SQL Server使用ISO 8601:

If you want to be more explicit, you can use the CONVERT function with a format type of 126, explicitly telling SQL Server to use ISO 8601:

declare @timeStr nvarchar(50) = CONVERT(nvarchar(50), SYSDATETIMEOFFSET(), 126)
select right(@timeStr, 6)

这两种方法在我的时区都返回:

Both of these approaches in my time zone return:

-06:00

有关 CAST 和 CONVERT 的更多信息请参见此处.

For more information about CAST and CONVERT see here.

这篇关于如何从 DateTimeOffset 对象中选择 UTC 偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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