从 varchar(36) 到 UNIQUEIDENTIFIER

发布于 2024-08-29 15:03:43 字数 348 浏览 4 评论 0原文

我正在尝试将 UNIQUEIDENTIFIER 的 AuctionId 转换为 varchar(36),然后再转换回 UNIQUEIDENTIFIER。请帮我。

CAST((SUBSTRING(CAST([AuctionId] as VARCHAR(36)), 0, 35) + '1') AS UNIQUEIDENTIFIER)

但我不断收到此错误:

消息 8169,16 级,状态 2,第 647 行 转换自时转换失败 一个字符串到 唯一标识符。

提前致谢

I am trying to cast an AuctionId that is a UNIQUEIDENTIFIER to an varchar(36) and then back to an UNIQUEIDENTIFIER. Please help me.

CAST((SUBSTRING(CAST([AuctionId] as VARCHAR(36)), 0, 35) + '1') AS UNIQUEIDENTIFIER)

But I keep getting this error:

Msg 8169, Level 16, State 2, Line 647
Conversion failed when converting from
a character string to
uniqueidentifier.

Thanks in advance

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

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

发布评论

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

评论(3

素罗衫 2024-09-05 15:03:43

“1”不是问题。显然,您试图将 GUID 的最后一个字符更改为 1。我不知道为什么,但这就是您的要求。

您的问题与子字符串有关。在 TSQL 中,子字符串使用从 1 开始的索引,而不是像 C 或 C# 中那样从 0 开始。这意味着您的子字符串语句实际上返回一个 34 个字符的字符串(+1 个字符等于 35 个字符,并且您被告知 35 个字符的字符串不是 GUID,这是正确的)。

只需将 ,0,35 更改为 1,35

The '1' is not the problem. You are obviously trying to change the last character of the GUID to a 1. I don't know why, but that's your requirement.

Your issue is with substring. In TSQL the substring uses an index starting at 1 not 0 like in C or C#. This means your substring statement is actually returning a 34 character string (+1 more character makes 35, and you're being told a 35 character string is not a GUID, which is right).

Just change the ,0,35 to 1,35

请持续率性 2024-09-05 15:03:43

您的错误是由于您的 +'1' 和您的 SUBSTRING 造成的。你把它放在那里做什么?

这会工作得很好

SELECT cast((cast(NEWID() as varchar(36))) as UNIQUEIDENTIFIER)

编辑:好的,所以如果你想用“1”替换最后一个字符,那么这就是解决方案

SELECT CAST(SUBSTRING(CAST(NEWID() AS VARCHAR(36)), 1, 35) + '1' AS UNIQUEIDENTIFIER)

唯一的区别是SQL中的SUBSTRING从位置1开始,而不是像你那样从位置0开始。

PS 这是危险的代码。输出不再是 GUID,因为它不符合用于生成 GUID 的算法。这可能(尽管不太可能)导致与 GUID 的冲突,从而可能导致各种问题。

Your error is due to your +'1' and your SUBSTRING. What do you have that in there for?

This will work fine

SELECT cast((cast(NEWID() as varchar(36))) as UNIQUEIDENTIFIER)

EDIT: Ok, so if you want to replace the last char with a '1' then this is the solution

SELECT CAST(SUBSTRING(CAST(NEWID() AS VARCHAR(36)), 1, 35) + '1' AS UNIQUEIDENTIFIER)

The only difference is that SUBSTRING in SQL starts at position 1, not position 0 as you had it.

P.S. This is dangerous code. The output is no longer a GUID as it will not conform to the algorithm that was used to generate the GUID. This could (although unlikely) result in a collision with GUIDs which could potentially cause all manner of problems.

时光与爱终年不遇 2024-09-05 15:03:43

正如其他人所观察到的,目前还不清楚你为什么要做你正在做的事情。

SUBSTRING 的替代方法是 STUFF 命令:

SELECT stuff(cast([AuctionId] as varchar(36)),36,1,'1')< /代码>

As others have observed, it's not clear why you want to do what you're doing.

An alternative to SUBSTRING is the STUFF command:

SELECT stuff(cast([AuctionId] as varchar(36)),36,1,'1')

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