在 SQL Server 中将 varchar 转换为 uniqueidentifier

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1390109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:29:58  来源:igfitidea点击:

Convert varchar to uniqueidentifier in SQL Server

sqlsql-server-2005uniqueidentifier

提问by grenade

A table I have no control of the schema for, contains a column defined as varchar(50) which stores uniqueidentifiers in the format 'a89b1acd95016ae6b9c8aabb07da2010' (no hyphens)

我无法控制其架构的表包含一个定义为 varchar(50) 的列,该列以“a89b1acd95016ae6b9c8aabb07da2010”(无连字符)格式存储唯一标识符

I want to convert these to uniqueidentifiers in SQL for passing to a .Net Guid. However, the following query lines don't work for me:

我想将这些转换为 SQL 中的 uniqueidentifiers 以传递给 .Net Guid。但是,以下查询行对我不起作用:

select cast('a89b1acd95016ae6b9c8aabb07da2010' as uniqueidentifier)
select convert(uniqueidentifier, 'a89b1acd95016ae6b9c8aabb07da2010')

and result in:

并导致:

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

The same queries using a hyphenated uniqueidentifier work fine but the data is not stored in that format.

使用带连字符的 uniqueidentifier 的相同查询工作正常,但数据未以该格式存储。

Is there another (efficient) way to convert these strings to uniqueidentifiers in SQL. -- I don't want to do it in the .Net code.

是否有另一种(有效)方法将这些字符串转换为 SQL 中的唯一标识符。-- 我不想在 .Net 代码中这样做。

回答by Quassnoi

DECLARE @uuid VARCHAR(50)
SET @uuid = 'a89b1acd95016ae6b9c8aabb07da2010'
SELECT  CAST(
        SUBSTRING(@uuid, 1, 8) + '-' + SUBSTRING(@uuid, 9, 4) + '-' + SUBSTRING(@uuid, 13, 4) + '-' +
        SUBSTRING(@uuid, 17, 4) + '-' + SUBSTRING(@uuid, 21, 12)
        AS UNIQUEIDENTIFIER)

回答by Hafthor

It would make for a handy function. Also, note I'm using STUFF instead of SUBSTRING.

这将是一个方便的功能。另外,请注意我使用的是 STUFF 而不是 SUBSTRING。

create function str2uniq(@s varchar(50)) returns uniqueidentifier as begin
    -- just in case it came in with 0x prefix or dashes...
    set @s = replace(replace(@s,'0x',''),'-','')
    -- inject dashes in the right places
    set @s = stuff(stuff(stuff(stuff(@s,21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-')
    return cast(@s as uniqueidentifier)
end

回答by manji

your varchar col C:

你的 varchar 列 C:

SELECT CONVERT(uniqueidentifier,LEFT(C, 8)
                                + '-' +RIGHT(LEFT(C, 12), 4)
                                + '-' +RIGHT(LEFT(C, 16), 4)
                                + '-' +RIGHT(LEFT(C, 20), 4)
                                + '-' +RIGHT(C, 12))

回答by Matthew

SELECT CONVERT(uniqueidentifier,STUFF(STUFF(STUFF(STUFF('B33D42A3AC5A4D4C81DD72F3D5C49025',9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-'))

回答by Sven

If your string contains special characters you can hash it to md5 and then convert it to a guid/uniqueidentifier.

如果您的字符串包含特殊字符,您可以将其散列到 md5,然后将其转换为 guid/uniqueidentifier。

SELECT CONVERT(UNIQUEIDENTIFIER, HASHBYTES('MD5','~?ü?a89b1acd95016ae6b9c8aabb07da2010'))

回答by user3082965

The guid provided is not correct format(.net Provided guid).

提供的 guid 格式不正确(.net 提供的 guid)。

begin try
select convert(uniqueidentifier,'a89b1acd95016ae6b9c8aabb07da2010')
end try
begin catch
print '1'
end catch