从 varchar(36) 到 UNIQUEIDENTIFIER
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“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
to1,35
您的错误是由于您的 +'1' 和您的 SUBSTRING 造成的。你把它放在那里做什么?
这会工作得很好
编辑:好的,所以如果你想用“1”替换最后一个字符,那么这就是解决方案
唯一的区别是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
EDIT: Ok, so if you want to replace the last char with a '1' then this is the solution
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.
正如其他人所观察到的,目前还不清楚你为什么要做你正在做的事情。
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 theSTUFF
command:SELECT stuff(cast([AuctionId] as varchar(36)),36,1,'1')