从 UniqueIdentifier 转换为 BigInt 并返回? [英] Convert from UniqueIdentifier to BigInt and Back?

查看:52
本文介绍了从 UniqueIdentifier 转换为 BigInt 并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ) )

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

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.

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

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().

推荐答案

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

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

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

(1 row(s) affected)


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

(1 row(s) affected)


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

(1 row(s) affected)

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

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

这篇关于从 UniqueIdentifier 转换为 BigInt 并返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆