加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

如何在两个二进制类型之间的sql server中进行按位异或?

发布时间:2020-12-12 06:28:46 所属栏目:MsSql教程 来源:网络整理
导读:根据这个链接: Bitwise Operators (Transact-SQL) 我们可以在binary和int,smallint,tinyint之间进行按位运算,反之亦然. 但是如何在两个二进制类型之间的sql server中进行按位异或? 或者,如果这不可能,我如何将二进制/ varbinary拆分为单个字节? 我要求这个
根据这个链接:
Bitwise Operators (Transact-SQL)
我们可以在binary和int,smallint,tinyint之间进行按位运算,反之亦然.

但是如何在两个二进制类型之间的sql server中进行按位异或?
或者,如果这不可能,我如何将二进制/ varbinary拆分为单个字节?

我要求这个的原因是因为我需要xor两个大于max int值的数字.
谢谢.

解决方法

代码块中的所有注释
-- variables
declare @vb1 binary(16),@vb2 binary(16),@lo binary(8),@hi binary(8)

-- 2 guids to compare
declare @guid1 uniqueidentifier set @guid1 = '96B4316D-1EA7-4CA3-8D50-FEE8047C1329'
declare @guid2 uniqueidentifier set @guid2 = 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'

-- split every 8 bytes into a binary(8),which is a bigint,the largest size usable with XOR
select @vb1 = @guid1,@vb2 = @guid2

-- xor the high and low parts separately
select @hi = convert(binary(8),substring(@vb1,1,8)) ^ convert(bigint,substring(@vb2,8))
select @lo = convert(binary(8),9,8))

-- the final result,concatenating the bytes using char(8) - binary -> uniqueidentifier
select 'A',@guid1 union all
select 'B',@guid2 union all
select 'A XOR B = ',convert(uniqueidentifier,convert(binary(16),convert(char(8),@hi) + convert(char(8),@lo)))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读