SQL 从 UniqueIdentifier 转换为 BigInt 并返回?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10078209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:15:22  来源:igfitidea点击:

Convert from UniqueIdentifier to BigInt and Back?

sqlsql-serversql-server-2008tsqluniqueidentifier

提问by Snowy

declare @uu uniqueidentifier =  'C50B0567-F8CC-4219-A1E1-91C97BD9AE1B'
select @uu
declare @zaza bigint = ( select convert(bigint, convert (varbinary(8), @uu, 1)) )
select @zaza
select CONVERT( uniqueidentifier , convert( varbinary(16) , @zaza , 1 ) )

I thought I had a fast way to convert Unique Identifier values to a Big Int, and back. But there is a problem in my second convert. Can anyone comment on the right way to fully convert a GUID to a number and back? I am only getting part of the GUID and not the whole thing when I try to convert it back from the numeric representation to its original GUID.

我以为我有一种快速的方法可以将唯一标识符值转换为 Big Int,然后再返回。但是我的第二次转换有问题。任何人都可以评论将 GUID 完全转换为数字并返回的正确方法吗?当我尝试将其从数字表示形式转换回其原始 GUID 时,我只得到了 GUID 的一部分,而不是全部。

I want to pass an integer (I think it would be classified as a "Large BigInt" in MSSQL?) to a remote system and just use characters 0-9, and still get the random uniqueness of NewId().

我想将一个整数(我认为它在 MSSQL 中被归类为“Large BigInt”?)传递给远程系统,并且只使用字符 0-9,并且仍然获得 NewId() 的随机唯一性。

回答by galets

There is no problem with your second convert. When I run your SQL statement in SQL management studio, I get:

你的第二次转换没有问题。当我在 SQL management studio 中运行你的 SQL 语句时,我得到:

------------------------------------
C50B0567-F8CC-4219-A1E1-91C97BD9AE1B

(1 row(s) affected)


--------------------
7423352504965404994

(1 row(s) affected)


------------------------------------
C50B0567-F8CC-4219-0000-000000000000

(1 row(s) affected)

Since you are converting 8 byte value to 16-byte guid, half of guid will be zeroes, which is exactly what you are seeing.

由于您将 8 字节值转换为 16 字节 guid,因此 guid 的一半将为零,这正是您所看到的。