Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an elegant way to Invert a Bit value in an SQL insert Statement?

I'm converting some data in SQL Server:

INSERT INTO MYTABLE (AllowEdit) (Select PreventEdit from SOURCETABLE) 

so I need to inverse the bit value from source table. I expected NOT to work, as this is how I would do it in code, but it doesn't. The most elegant way I can think of is:

INSERT INTO MYTABLE (AllowEdit) (Select ABS(PreventEdit -1) from SOURCETABLE) 

Is there a more standard way to do it?

like image 376
Molloch Avatar asked Jul 24 '10 10:07

Molloch


1 Answers

I did not test this myself, but you should be able to use the bitwise negation operator, ~ on a bit:

INSERT INTO MYTABLE (AllowEdit)  (SELECT ~PreventEdit FROM SourceTable) 
like image 112
driis Avatar answered Oct 15 '22 20:10

driis