SQL Server WHERE子句中的“&"运算符 [英] Ampersand (&) operator in a SQL Server WHERE Clause
问题描述
很抱歉,这是一个非常基本的问题.&
运算符在此SQL中做什么
Sorry for the very basic question. What does the &
operator do in this SQL
WHERE (sc.Attributes & 1) = 0
sc
是表的别名,该表包含列属性
.
sc
is an alias for a table which contains a column attributes
.
我试图了解报表中的某些SQL,并且该行使其返回0个条目.如果我将其注释掉,它将起作用.我对SQL的了解有限,我不确定&是什么.1
正在执行.
I'm trying to understand some SQL in a report and that line is making it return 0 entries. If I comment it out it works. I have limited SQL knowledge and I'm not sure what the & 1
is doing.
推荐答案
&是按位逻辑和运算符-它对2个整数值执行运算.
& is the bitwise logical and operator - It performs the operation on 2 integer values.
WHERE (sc.Attributes & 1) = 0
上面的代码检查sc.Attributes是否为偶数.这与说未设置第一位相同.
The above code checks to see if sc.Attributes is an even number. Which is the same as saying that the first bit is not set.
由于该列的名称为:"Attributes",因此"1"值可能只是一些具有某些外部含义的标志.
Because of the name of the column though: "Attributes", then the "1" value is probably just some flag that has some external meaning.
对于存储在属性编号中的每个标志,通常使用1个二进制数字.因此,要测试第一位,请使用sc.Attributes& 1,测试第二位,请使用sc.Attributes& 2,测试第三位,请使用sc.Attributes& 4,测试第四位,请使用sc.属性& 8,...
It is common to use 1 binary digit for each flag stored in a number for attributes. So to test for the first bit you use sc.Attributes&1, to test for the second you use sc.Attributes&2, to test for the third you use sc.Attributes&4, to test for the fourth you use sc.Attributes&8, ...
= 0部分正在测试以查看是否未设置第一位.
The = 0 part is testing to see if the first bit is NOT set.
一些二进制示例:(==显示操作结果)
Some binary examples: (== to show the result of the operation)
//Check if the first bit is set, same as sc.Attributes&1
11111111 & 00000001 == 1
11111110 & 00000001 == 0
00000001 & 00000001 == 1
//Check if the third bit is set, same as sc.Attributes&4
11111111 & 00000100 == 1
11111011 & 00000100 == 0
00000100 & 00000100 == 1
这篇关于SQL Server WHERE子句中的“&"运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!