Boolean 'NOT' in T-SQL not working on 'bit' datatype?
⚡TLDR
To flip the bit
state in T-SQL, exploit the ~
bitwise operator:
If you are looking for explicit false values, here you go:
Look for both 0 and NULL
? That's your choice:
Working with bitwise negation
Working with bit
datatype and need negation? ~
operator is your new best friend:
The advantage of '~'
While you can play with subtraction:
Going with the ~
operator is like negation on steroids — straightforward, concise, and clear:
Tips and traps
Dealing with NULL
Beware, the beast NULL
is still NULL
after bitwise NOT:
Check your 'NOT's
In conditions, use NOT
wisely with explicit checks:
Advanced Operations
Pairing with CASE
statement
Negation in a conditional play:
Handy in JOINs
The bitwise NOT for enhancing joins:
Indexes & negation
Fight-off performance hits. Avoid using negation in indexed columns.
Linked
Was this article helpful?