113

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)

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:

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.

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

grenade
  • 28,964
  • 22
  • 90
  • 125
  • just a row of characters and numbers is really not a valid GUID representation - you'll have to resort to string parsing magic like Quassnoi showed in his answer. – marc_s Sep 07 '09 at 17:07

6 Answers6

131
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)
Quassnoi
  • 381,935
  • 83
  • 584
  • 593
  • 13
    I was really hoping this would not be the solution but I guess we'll find out soon... – grenade Sep 07 '09 at 16:39
  • 26
    DECLARE @u uniqueidentifier SELECT @u = CONVERT(uniqueidentifier, 'c029f8be-29dc-41c1-8b38-737b4cc5a4df') *** This would be enough. Just tried it. – Fabio Milheiro Apr 14 '11 at 00:29
  • Oh yes! Then I have to agree. The obvious thing is to put the hyphens in the right places and you're ready to go! Sorry! – Fabio Milheiro Apr 14 '11 at 18:21
  • Placing this snippet in a function is a nice addition to your toolkit, especially since some JSON serializers remove the dashses from GUIDs when serializing, making it harder to copy paste into SQL to debug. – David Cumps Jan 23 '13 at 09:41
27

It would make for a handy function. Also, note I'm using STUFF instead of 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
Hafthor
  • 15,081
  • 9
  • 54
  • 62
  • 4
    Excellent use of Stuff(). I only need to reference my field once in a Select statement using your method. I avoid Scalar-Functions though, because they don't always quite "scale" well, so I write it out. Thanks, this is going in my Code Snippits! – MikeTeeVee Jun 27 '13 at 01:38
18

your varchar col 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))
manji
  • 45,615
  • 4
  • 87
  • 100
12
SELECT CONVERT(uniqueidentifier,STUFF(STUFF(STUFF(STUFF('B33D42A3AC5A4D4C81DD72F3D5C49025',9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-'))
Matthew
  • 161
  • 1
  • 3
-4

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

SELECT CONVERT(UNIQUEIDENTIFIER, HASHBYTES('MD5','~öü߀a89b1acd95016ae6b9c8aabb07da2010'))
Sven
  • 9,958
  • 2
  • 14
  • 19
-6

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

begin try
select convert(uniqueidentifier,'a89b1acd95016ae6b9c8aabb07da2010')
end try
begin catch
print '1'
end catch
ChrisM
  • 488
  • 4
  • 15
  • 7
    How does this answer the question of converting a varchar without hyphens to a GUID? All this code does is print 1. – Aaroninus Sep 11 '15 at 15:44