Cómo voltear los campos de bits en T-SQL?
Estoy tratando de voltear un campo de bits en SQL Server usando una consulta de actualización, es decir, quiero convertir todos los 0 en 1 y viceversa. ¿Cuál es la solución más elegante?
No parece haber un operador bitwise NOT en T-SQL (a menos que me esté perdiendo algo obvio) y no he podido encontrar ninguna otra forma de realizar la actualización.
8 answers
No necesitas un bitwise-no para esto just solo XOR con 1 / true.
Para comprobarlo:
select idColumn, bitFieldY, bitFieldY ^ 1 as Toggled
from tableX
Para actualizar:
update tableX
set bitFieldY = bitFieldY ^ 1
where ...
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
2009-09-09 03:10:51
¿Por qué no un simple bitfield = 1 - bitfield
?
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
2009-09-09 05:00:38
Otra forma es
DECLARE @thebit bit = 1, @theflipbit bit
SET @theflipbit = ~ @thebit
SELECT @theflipbit
Donde "~" significa "NO" operador. Está limpio y tienes un buen código para leer. "negar la broca "es aún más limpio y hace exactamente lo que el operador" NO " fue diseñado para.
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
2015-06-18 20:49:29
Estaba bastante seguro de que la mayoría de los sabores SQL tenían un bitwise NO, así que comprobé y no parece ser uno en TSQL.
De la documentación, es el carácter ~
.
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-10-01 02:32:04
UPDATE tblTest SET MyBitField = CASE WHEN MyBitField = 1 THEN 0 ELSE 1 END
Es soso pero todos entenderán lo que está haciendo.
EDITAR:
Es posible que también tenga que tener en cuenta los nulos como se sugiere en los comentarios. Depende de sus requisitos, por supuesto.
UPDATE tblTest SET
MyBitField = CASE
WHEN MyBitField = 1 THEN 0
WHEN MyBitField = 0 THEN 1
ELSE NULL -- or 1 or 0 depending on requirements
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
2009-09-09 03:29:27
Un simple operador bitwise NOT (~) funcionó para mí en SQL Server 2014-12.0.2269.0
En la cláusula update dentro de su T-SQL -
Update TableName
SET [bitColumnName] = ~[bitColumnName],
....
WHERE ....
Espero que esto ayude
Ref - https://docs.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-not-transact-sql
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
2017-06-15 09:58:24
¿Probaste esto?
UPDATE mytable SET somecolumn =
CASE WHEN somecolumn = 0 THEN 1
WHEN somecolumn IS NULL THEN NULL
WHEN somecolumn = 1 THEN 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
2009-09-09 03:21:03
Consulta (vb)
x = "select x from table"
Actualización (vb)
"update table set x=" Not(x*(1))
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-08-01 05:44:31