0

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
4

1 回答 1

0

不幸的是,我还没有偶然发现从 a 中的十六进制值转换为一次循环一个数字VARCHARNUMERIC短。

declare @GUID as UniqueIdentifier = NewId()
declare @Binary as VarBinary(64) = @GUID
declare @String as VarChar(64) = Convert( VarChar(64), @Binary, 2 )

select @GUID as 'GUID', @Binary as 'Binary', @String as 'String'
于 2012-11-14T22:34:07.053 回答