Is there an XOR operator or equivalent function in SQL Server (T-SQL)?
<>
is generally a good replacement for XOR wherever it can apply to booleans.
The xor operator is ^
For example: SELECT A ^ B
where A and B are integer category data types.
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
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
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))
MS SQL only short form (since SQL Server 2012):
1=iif( a=b ,1,0)^iif( c=d ,1,0)