Sunday, April 11, 2010

Swap bit values of a column using update query

Suppose there is a situation in which
There is column named pass/fail.which has default values 1/0
Now by mistake you have inserted it as 0 for pass and 1 for fail.
Now you need to update but you cannot do it easily because,

If i write query like this "update result set pass=1 where pass=0"
or update result set fail=0 where fail=1
this will surely create a havoc.
So, we need to toggle both bit values at a time
For which you can write it as
update result set pass=pass ^1 
this is called XOR operation

This will do the task happy Querying...:-)

1 comment:

  1. This is a kool idea...i never knew this...

    but if it is a bit..

    instead if it would have been an integer and you wanted to swap...

    for eg. You want to swap 1 and 2 with each would you do that...