NOT operator in SQL Server
The NOT operator in SQL Server is used to negate a Boolean expression in the WHERE clause of a SELECT, UPDATE, or DELETE statement. It returns true if the expression is false and false if the expression is true. The NOT operator is used in combination with other operators to give the opposite result, also called the negative result.
Here is an example of how to use the NOT operator in SQL Server:
Syntax: -
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
The NOT operator can be used with other operators such as LIKE, IN, and BETWEEN. For example, the following SQL statement selects all customers whose name does not start with the letter ‘G’:
SELECT * FROM Customers
WHERE NOT CustomerName LIKE 'G%';
Select only the customers that are NOT from Spain:
SELECT * FROM Customers
WHERE NOT Country = ‘Spain’;
- NOT LIKE
Select customers that does not start with the letter ‘A’:
SELECT * FROM Customers
WHERE CustomerName NOT LIKE ‘A%’;
- NOT BETWEEN
Select customers with a customerID not between 10 and 60:
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;
- NOT IN
Select customers that are not from Paris or London:
SELECT * FROM Customers
WHERE City NOT IN (‘Paris’, ‘London’);
- NOT Greater Than
Select customers with a CustomerId not greater than 50:
SELECT * FROM Customers
WHERE NOT CustomerID > 50;
SELECT * FROM Customers
WHERE NOT CustomerId < 50;