I need to do some math with SQL Server GUIDs in a trigger and I'm having difficulty figuring out how to convert a uniqueidentifier
to a numeric(38,0)
.
One potential problem: my understand is that both of these datatypes are 16-byte "integers". If I'm wrong here, please correct me.
Otherwise, how would I go about this conversion? I've tried CAST and CONVERT and keep getting Explicit conversion from data type uniqueidentifier to numeric is not allowed. as an error message whenever I try. I'd really like to not have to parse each character and do hex math in a UDF to do this.
Is this possible?
Here's my script to repro this real quick:
DECLARE @guid uniqueidentifier
SET @guid = NEWID()
DECLARE @a numeric(38,0)
SET @a = 2
PRINT CAST(@guid AS numeric(38,0)) -- fails
PRINT @guid / @a -- also fails