In this post explains how to use the NOT condition in SQL Server (Transact-SQL) with syntax and examples.
The SQL Server (Transact-SQL) NOT condition, also called the NOT Operator, is used to negate a condition in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the NOT condition in SQL Server (Transact-SQL) is:
The condition to negate.
The SQL Server NOT condition can be combined with the IN condition.
For example:
This SQL Server NOT example would return all rows from the employees table where the first_name is not 'John', 'Dale', or 'Susan'. Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.
The SQL Server NOT condition can also be combined with the IS NULL condition.
For example,
This SQL Server NOT example would return all records from the employees table where the last_name does not contain a NULL value.
The SQL Server NOT condition can also be combined with the LIKE condition.
For example:
By placing the SQL Server NOT Operator in front of the LIKE condition, you are able to retrieve all employees whose last_name does not start with 'A'.
The SQL Server NOT condition can also be combined with the BETWEEN condition. Here is an example of how you would combine the NOT Operator with the BETWEEN condition.
For example:
This SQL Server NOT example would return all rows from the employees table where the employee_id was NOT between 200 and 250, inclusive. It would be equivalent to the following SQL Server SELECT statement:
The SQL Server NOT condition can also be combined with the EXISTS condition.
For example,
This SQL Server NOT example would return all records from the employees table where there are no records in the contacts table for the matching last_name and first_name.