Mar 9 2006

How to use "NOT" in SQL Queries

Category:Bil@l @ 20:58

While wokring with C# or VB.NET, we sometimes implement a switcher-statement. For example, in a certain if statement, we need to flip the state of a certain boolean value as:

bool isValid;
if (State == 2)
{
    isValid = !isValid;
}

This way, we will flip the value of isValid from false to true whenever the value of State equals 2.

In one of the stored procedures I was working on, I needed a way to Flip the value of a paramter in a table I have, and this parameter is of type bit, so how can we accomplish this in SQL? The answer is so simple, check the example below:

UPDATE
    MyTable
SET
    IsAutoPayment = ~IsAutoPayment
WHERE 
    CustomerId = @CustomerId

That is very useful when you want to change state of a parameter from one state to another,

So, as a conclusion the (~) is equivalent to (!) or (Not) in C# and VB.NET respectively.

Hope this helps,

Regards

Tags:

Comments are closed