将区域字符串转换为 datetimeoffset

发布于 2025-01-17 01:51:38 字数 214 浏览 0 评论 0原文

我有一个以下格式的日期字符串:

9/28/2006 5:53:03 PM

我需要将其转换为与 datetimeoffset 列兼容的格式(例如 2006-09-28 17:53:03 GMT )。

我找不到任何转换函数来获取这种格式。寻找 Excel 或 SQL Server 转换。

I have a date string in the following format:

9/28/2006 5:53:03 PM

I need to convert this to a format compatible with a datetimeoffset column (e.g. 2006-09-28 17:53:03 GMT).

I couldn't find any convert function to get this format. Looking either for Excel or SQL Server conversion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-01-24 01:51:38

要获得您在问题中要求的显式字符串输出,您可以这样做,但是该格式与 datetimeoffset 不兼容,主要是因为 GMT 在那里无效。

DECLARE @string varchar(50) = '9/28/2006 5:53:03 PM';

SELECT CONVERT(varchar(50), CONVERT(datetime2(0), @string, 101)) + ' GMT';

String 输出:

2006-09-28 17:53:03 GMT

由于您实际上似乎根本不需要您询问的格式,而是需要将此值插入到 datetimeoffset 列中,因此您的显式格式问题中询问的内容无关紧要。尝试(不清楚您的所有时间是否都是 UTC 且这就是您想要的,或者您是否真的打算转换为 GMT,遵守 DST IIRC):

DECLARE @string varchar(50) = '9/28/2006 5:53:03 PM';

DECLARE @dt datetimeoffset = CONVERT(datetime, @string, 101);

SELECT [UTC] = @dt, 
       [GMT] = @dt AT TIME ZONE 'GMT Standard Time';

输出

UTCGMT
2006-09-28 17:53:03.0000000 +00: 002006-09-28 18:53:03.0000000 +01:00

那应该是与您的 datetimeoffset 列兼容。下次请解释整个问题。

在任何一种情况下,请不要使用 FORMAT() (如评论中建议的那样),尤其是在大规模情况下。

To get the explicit string output you asked for in the question, you can do this, however that format is not compatible with datetimeoffset, mainly because GMT is invalid there.

DECLARE @string varchar(50) = '9/28/2006 5:53:03 PM';

SELECT CONVERT(varchar(50), CONVERT(datetime2(0), @string, 101)) + ' GMT';

String output:

2006-09-28 17:53:03 GMT

Since you actually don't seem to want the format you asked about at all and rather need to insert this value into a datetimeoffset column, then the explicit format you ask about in the question is irrelevant. Try (and not clear if all your times are in UTC and that's what you want, or if you really meant to translate to GMT, which observes DST IIRC):

DECLARE @string varchar(50) = '9/28/2006 5:53:03 PM';

DECLARE @dt datetimeoffset = CONVERT(datetime, @string, 101);

SELECT [UTC] = @dt, 
       [GMT] = @dt AT TIME ZONE 'GMT Standard Time';

Output

UTCGMT
2006-09-28 17:53:03.0000000 +00:002006-09-28 18:53:03.0000000 +01:00

That should be compatible with your datetimeoffset column. Next time please explain the entire problem.

In either case, please don't use FORMAT() for this (as suggested in a comment), especially at scale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文