Search code examples
sql-serverssisbitwise-operatorsbitwise-or

How does not | (Bitwise OR) work in SQL Server/SSIS 2012?


I've read about the Bitwise OR and it seems to be like it functions same as OR except it's faster.

I read https://msdn.microsoft.com/en-us/library/ms186714(v=sql.110).aspx

And here is the example they give:

USE tempdb;
GO

SELECT a_int_value | b_int_value
FROM tableName

How is this expected to run? It makes no sense, you cannot have an OR in the select statement

1) Am I missing something? 2) Is it safe to say that if my comparison expressions are of type integer, I should always use bitwise OR? (this is because bitwise OR is faster and works only on integer comparisons?)


Solution

  • I don't have a whole lot of experience with that flavor of SQL, but a bitwise OR is not the same thing as an OR clause in the WHERE statement.

    The bitwise OR will OR each bit of the integer together to produce a new integer For example, the numbers 2 and 9 can be represented in binary as 0010 and 1001. So therefore

    0010 | 1001 = 1011
    

    In other words

    2 | 9 = 11
    

    The | operator in your statement is performing this operation on the results.

    Please note that this operation is not equivalent to addition i.e.

    5(0101) | 3(0011) = 7(0111)