daniel_aren
daniel_aren

Reputation: 1924

What does "&" means in this sql where clause?

What does the "&" mean here:

select pt.TrTp, sum(pt.TrTp)
from ProdTr pt
where TransSt & 16 <> 16  // this is the row that i don´t understand..
group by pt.TrTp

Upvotes: 1

Views: 10072

Answers (5)

0xCAFEBABE
0xCAFEBABE

Reputation: 5666

It's called a bitmask. It is used in situations where the individual bits in a number have different meanings, as opposed to a number just meaning the number itself (for instance, if you save your age to the database).

When you imagine any number in it's binary form and want to test if a certain bit in the number is set, you test it by using the binary AND operator with the number and the bit you want to test, like this:

if (number & 16 == 16)

In binary, this means the following (assuming, your number is 25):

if (00011001 & 00010000 == 00010000)

Here you can see, that the digits at the bit 5 (counted from the bottom up) are both 1, therefor the resulting number has a 1 at that bit. As there are no other 1s, the resulting number is 16 exactly when both numbers have a 1 at this position.

I would like to add: It's usually bad practice to encode different meanings into one database field. It's difficult to index and retrieve by an index, and depending on your DBMS might even be completely unindexed.

Upvotes: 7

user1788978
user1788978

Reputation: 306

It's a bitwise AND operator.

A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power/performance because of the reduced use of resources.

From http://en.wikipedia.org/wiki/Bitwise_operation

Upvotes: 0

Andrew Savinykh
Andrew Savinykh

Reputation: 26300

It's bitwise AND. Please refer to this MSDN Article.

Upvotes: 0

mvp
mvp

Reputation: 116337

This is bitwise AND. It simply checks if bit 4 in value is NOT set.

I would rewrite this expression as TransSt & 16 = 0

Upvotes: 1

J. Steen
J. Steen

Reputation: 15578

It's a bitwise AND.

The & bitwise operator performs a bitwise logical AND between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if and only if both bits (for the current bit being resolved) in the input expressions have a value of 1; otherwise, the bit in the result is set to 0.

From http://msdn.microsoft.com/en-us/library/ms174965.aspx

In this case it's used with a flag, where multiple values are stored in one field and then the bitwise AND operation is used to check for specific states - or in this particular case - check that the field doesn't contain the specified state.

Upvotes: 0

Related Questions