Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Casting a String GUID to HEX

Author  Topic 

iliad
Starting Member

2 Posts

Posted - 2009-01-14 : 06:35:03
Hi!

I would like to have the HEX value of a GUID but something is going wrong. I'm very new to this and have googled already, but no success.
print cast('5CB06756-3D58-41CD-AFAC-CAEFFE71C60E' as binary(16))
--> 0x35434230363735362D334435382D3431
print cast(0x35434230363735362D334435382D3431 as uniqueidentifier)
--> 30424335-3736-3635-2D33-4435382D3431

I take the '5CB06756-3D58-41CD-AFAC-CAEFFE71C60E' as a starting point, then I convert it to a hex and convert it back to a GUID. But I get '30424335-3736-3635-2D33-4435382D3431' instead of the original value. What is wrong?

Thanks for helping!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-14 : 06:44:09
Snippet taken from http://weblogs.sqlteam.com/peterl/archive/2008/10/13/Get-the-job-name-for-current-context.aspx
DECLARE @SQL NVARCHAR(100),
@ID UNIQUEIDENTIFIER,
@Hex VARCHAR(100),
@s VARCHAR(100)

SELECT @s = '5CB06756-3D58-41CD-AFAC-CAEFFE71C60E',
@SQL = 'SET @guid = CAST(' + QUOTENAME(@s, '''') + ' AS UNIQUEIDENTIFIER)'

EXEC sp_executesql @SQL, N'@guid UNIQUEIDENTIFIER OUT', @guid = @ID OUT

SELECT @ID



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

iliad
Starting Member

2 Posts

Posted - 2009-01-14 : 09:41:04
Peso, thx for the quick reply.

Could you explain this in a more easy way? Why can I not cast from a GUID to a hex and back?

Is 0x35434230363735362D334435382D3431 really the hex value of '5CB06756-3D58-41CD-AFAC-CAEFFE71C60E'?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-14 : 10:02:02
You can, but you will have to wait for SQL Server 2008.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -