tags:

views:

57

answers:

3

in c# i have a:

 a = !a

(if false makes it true, if true makes it false)

in sql i want to do the same with a BIT variable

something like:

 declare @a bit
 set @a = 1
 select @a
 set @a = not (@a)
 select @a

can i?

i could always do an IF, but this would "look better" :)

A: 

You could use the XOR operator for that.

declare @a bit
set @a = 1
select @a

set @a = @a ^ 1
select @a

set @a = @a ^ 1
select @a
Lieven
+6  A: 

try this by using a bitwise not

The ~ bitwise operator performs a bitwise logical NOT for the expression, taking each bit in turn. If expression has a value of 0, the bits in the result set are set to 1; otherwise, the bit in the result is cleared to a value of 0. In other words, ones are changed to zeros and zeros are changed to ones.

declare @a bit
 set @a = 0
 select @a
 set @a = ~@a
 select @a

make it 1

declare @a bit
 set @a = 1
 select @a
 set @a = ~@a
 select @a
SQLMenace
+1. I Learned something, thx.
Lieven
~ (Bitwise NOT) Returns the ones complement of the number.
Branimir
A: 

Here's the code:

set @a = @a^1
Vivek