T-SQL XOR Operator

后端 未结 9 558
既然无缘
既然无缘 2020-11-30 23:43

Is there an XOR operator or equivalent function in SQL Server (T-SQL)?

相关标签:
9条回答
  • 2020-12-01 00:22

    <> is generally a good replacement for XOR wherever it can apply to booleans.

    0 讨论(0)
  • 2020-12-01 00:27

    The xor operator is ^

    For example: SELECT A ^ B where A and B are integer category data types.

    0 讨论(0)
  • 2020-12-01 00:27

    It is ^ http://msdn.microsoft.com/en-us/library/ms190277.aspx

    See also some code here in the middle of the page How to flip a bit in SQL Server by using the Bitwise NOT operator

    0 讨论(0)
  • 2020-12-01 00:28

    Using boolean algebra, it is easy to show that:

    A xor B = (not A and B) or (A and not B)
    
    
    A B | f = notA and B | g = A and notB | f or g | A xor B    
    ----+----------------+----------------+--------+--------    
    0 0 | 0              | 0              | 0      | 0    
    0 1 | 1              | 0              | 1      | 1    
    1 0 | 0              | 1              | 1      | 1    
    1 1 | 0              | 0              | 0      | 0
    
    0 讨论(0)
  • 2020-12-01 00:29

    There is a bitwise XOR operator - the caret (^), i.e. for:

    SELECT 170 ^ 75
    

    The result is 225.

    For logical XOR, use the ANY keyword and NOT ALL, i.e.

    WHERE 5 > ANY (SELECT foo) AND NOT (5 > ALL (SELECT foo))
    
    0 讨论(0)
  • 2020-12-01 00:32

    MS SQL only short form (since SQL Server 2012):

    1=iif( a=b ,1,0)^iif( c=d ,1,0)
    
    0 讨论(0)
提交回复
热议问题