質問

I want to cast or convert newid() output to decimal (with minimum space required)

So far I did this :

SELECT  CAST( '0x'+REPLACE(CAST(NEWID() AS VARCHAR(50)), '-', '' ) AS DECIMAL(38,0) )

But I'm getting this error :

Error converting data type varchar to numeric.

Searched a lot but getting frustrated, came here to find some help.

Many thanks in advance.


update : decimal(8,0) became decimal(38,0) and still getting the same error.

役に立ちましたか?

解決

This is an xy problem.

The correct datatype to use for GUIDs is uniqueidentifier. There is no need to convert it to some numeric datatype yourself.

The uniqueidentifier is stored in a 16 byte binary format not the char(36) string that is displayed.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top