DECLARE @myguid uniqueidentifier = NEWID();
DECLARE @myguid36 VARCHAR(36)
SELECT @myguid36=REPLACE(CAST(@myguid AS VARCHAR(36)),'-','')
SELECT @myguid36=UPPER(REPLACE(CAST(@myguid AS VARCHAR(36)),'-',''))--转大写
SELECT @myguid36=LOWER(REPLACE(CAST(@myguid AS VARCHAR(36)),'-',''))--转小写
SELECT @myguid,@myguid36,CONVERT(uniqueidentifier,STUFF(STUFF(STUFF(STUFF(@myguid36,9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-'))
SELECT CONVERT(uniqueidentifier,LEFT(C, 8)
+ '-' +RIGHT(LEFT(C, 12), 4)
+ '-' +RIGHT(LEFT(C, 16), 4)
+ '-' +RIGHT(LEFT(C, 20), 4)
+ '-' +RIGHT(C, 12))
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
https://stackoverflow.com/questions/1390109/convert-varchar-to-uniqueidentifier-in-sql-server https://stackoverflow.com/questions/16222343/convert-guid-to-varchar32/16222390