Table of Contents
MS SQL Server: Conditions NOT in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The NOT condition in SQL Server (Transact-Server) is also called the NOT operator, which helps negate conditions in SELECT, INSERT, UPDATE, and DELETE statements.
Syntax of NOT condition
NOT '?i?u ki?n'
Variable name or variable value
condition
Conditions must be negative.
Note
The NOT condition requires that the record be contrary to the 'condition' to be returned in the result set.
Example - combined with IN conditions
SELECT *FROM nhanvienWHERE teNOT IN ('John', 'Dale', 'Susan');
This example will return all rows from the nhanvien table whose name is not John, Dale or Susan. Sometimes finding values that are not worth looking for is faster.
For instance, - combine condition IS NULL
This is an example of a combination of NOT and IS NULL conditions in SQL Server.
SELECT *FROM nhanvienWHERE ho IS NOT NULL;
The returned result is all records in the table nhanvien that they contain no NULL value.
Example - combine LIKE condition
Combine LIKE and NOT to find values against what you want.
SELECT nhanvien_id, ho, tenFROM nhanvienWHERE ho NOT LIKE 'A%';
When placing the NOT operator before the LIKE condition, the result will be for employees whose last name does not start with the letter 'A'.
For instance, - combine BETWEEN conditions
SELECT *FROM nhanvienWHERE nhanvien_id NOT BETWEEN 200 AND 250;
In this example, the returned result is the rows in the employee table that have nanvien_id not in the range of 200 and 250, including both the first and last values. It will be equivalent to the SELECT command below.
SELECT *FROM nhanvienWHERE nhanvien_id < 200OR nhanvien_id> 250;
For example - combine EXISTS conditions
SELECT *FROM nhanvienWHERE NOT EXISTS (SELECT *FROM danhbaWHERE nhanvien.ho = danhba.hoAND nhanvien.ten = danhba.ten);
Next lesson: ALIAS in SQL Server
FAQ
What is MS SQL Server: Conditions NOT in SQL Server?
The NOT condition in SQL Server (Transact-Server) is also called the NOT operator, which helps negate conditions in SELECT, INSERT, UPDATE, and DELETE statements.
Why is MS SQL Server: Conditions NOT in SQL Server important?
A clear understanding of MS SQL Server: Conditions NOT in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach MS SQL Server: Conditions NOT in SQL Server?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Reader Comments 0
Sign in with email or Google to join the discussion.