navigation
 Monday, September 22, 2008

This is embarassing. I've been coding in T-SQL for years, and when I've had to invert a bit, I've been writing stuff like this:

case @bit
    when 0 then cast ( 1 as bit )
    when 1 then cast ( 0 as bit )
    else null
end

because the NOT operator doesn't work on bits. Today, I thought to myself: "Bitwise NOT. Didn't I see that in Books Online?" And yep, there it is. Instead of typing the above to invert a bit, all you need is the bitwise NOT operator "~".

~@bit

Forehead, meet palm.