What does the pipe operator do in SQL?

点点圈 提交于 2019-11-29 14:14:37

from the MSDN documentation,

| (Bitwise OR) (Transact-SQL)

Performs a bitwise logical OR operation between two specified integer values as translated to binary expressions within Transact-SQL statements.

...

The bitwise | operator performs a bitwise logical OR between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if either or both bits (for the current bit being resolved) in the input expressions have a value of 1; if neither bit in the input expressions is 1, the bit in the result is set to 0.

If the left and right expressions have different integer data types (for example, the left expression is smallint and the right expression is int), the argument of the smaller data type is converted to the larger data type. In this example, the smallint expression is converted to an int.

for example, see this fiddle,

SELECT 1 | 1, 1 | 2, 2 | 4, 3 | 5;

outputs

1    3    6    7

to explain this behavior you must consider the bit patterns of the operands,

1 | 1

  00000001  = 1
| 00000001  = 1
_______________
  00000001  = 1

1 | 2

  00000001  = 1
| 00000010  = 2
_______________
  00000011  = 3

2 | 4

  00000010  = 2
| 00000100  = 4
_______________
  00000110  = 6

3 | 5

  00000011  = 3
| 00000101  = 5
_______________
  00000111  = 7

The | (pipe) operator in several dialects of SQL is the bitwise or operator.

In this usage, it is testing to make sure that the value of the column remains the same after applying the bitwise or on it. Another approach to doing the same thing is to use the bitwise and operator (&) to mask just the bits in question and test it against the mask.

I personally find the and to be more idiomatic than the or approach, but thats me.

An SQL Fiddle demonstrating the results: http://sqlfiddle.com/#!6/aeb46/4

The contents of the fiddle are:

create table foo (bits integer);

insert into foo values (1), (2), (3), (4), (5), (6), (7), (8), (9)

and

select bits from foo where (bits | 2) = bits;
select bits from foo where (bits | 8) = bits;

select bits from foo where (bits & 2) = 2;
select bits from foo where (bits & 8) = 8;

The query for the 2 returns 2, 3, 6, 7 while the query for the 8 returns 8, 9.

If you really wanted to, the test for if the 8's bit is set can also be done as:

select bits from foo where (bits / 8) % 2 = 1

But that's just a bit on the silly side, but it works.

In languages that don't use the C style bitwise operators, there is often a similar function to do the bitwise work. For example, in Oracle one would use BITAND and BITOR (see BITAND in the oracle docs for how that would be used) - the & in Oracle SQL dialect is the indication of a parameter and a || is for string concatenation.

I'd suspect that the | is a bitwise OR operator. So you're ORing au.ExNetBits with binary 1000 - essentially checking that a single bit is set.

My comment guess was right, a quick Google for Bitwise OR shows that at least MySQL accepts | as a syntax for a bitwise OR: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html

EDIT: as @satanicpuppy points out, it is also valid syntax in SQL Server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!