Search code examples
sql-servert-sqlxor

T-SQL XOR Operator


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


Solution

  • 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))