Operador XOR de T-SQL
¿Hay un operador XOR o una función equivalente en SQL Server (T-SQL)?
7 answers
Hay un operador XOR bit a bit - el caret ( ^ ), es decir, para:
SELECT 170 ^ 75
El resultado es 225.
Para XOR lógico, use la palabra clave ANY y NO ALL, es decir,
WHERE 5 > ANY (SELECT foo) AND NOT (5 > ALL (SELECT foo))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-23 21:18:30
Usando álgebra booleana, es fácil demostrar que:
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-06 14:34:04
Como se aclaró en su comentario, Spacemoses, indicó un ejemplo: WHERE (Note is null) ^ (ID is null). No veo por qué ha optado por aceptar cualquier respuesta dada aquí como respuesta a eso. Si necesitara un xor para eso, creo que tendría que usar la lógica Y / O equivalente:
WHERE (Note is null and ID is not null) OR (Note is not null and ID is null)
Eso es equivalente a:
WHERE (Note is null) XOR (ID is null)
Cuando 'XOR' no está disponible.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-22 16:22:37
MS SQL solo forma corta (desde SQL Server 2012):
1=iif( a=b ,1,0)^iif( c=d ,1,0)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-16 13:56:47
El operador xor es ^
Por ejemplo: SELECT A ^ B
donde A y B son tipos de datos de categoría entera.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-18 20:06:54
Es ^
http://msdn.microsoft.com/en-us/library/ms190277.aspx
Vea también algún código aquí en el medio de la página Cómo voltear un bit en SQL Server usando el operador Bitwise NOT
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-23 21:14:07
De su comentario:
Ejemplo: WHERE (Note is null) ^ (ID is null)
Probablemente podrías probar:
where
(case when Note is null then 1 else 0 end)
<>(case when ID is null then 1 else 0 end)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-06 06:01:52