T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - NOT LIKE Keyword



The T-SQL (Transact-SQL) NOT LIKE keyword is the negation of LIKE keyword. The LIKE keyword is used in a WHERE clause to search for a specified pattern in a specified column. The wildcards which are used in conjunction with the LIKE or NOT LIKE keyword are given below:

Wildcard Characters in T-SQL

SymbolDescriptionExample
%Represents zero, one or multiple characters.'J%' represents a value that start with "J", for example - John, Jo and Jack etc.
_Represents one character.'_o%' represents a value that have "o" in the second position, for example - John, Jo and Journey etc.
[]Represents any single character specified within [].'[ack]%' represents a value that starts with a, c or k, for example - ant, cat or kite etc.
-Represents a range of characters.'[a-d]%' represents a value that starts with a, b, c or d, for example - ant, bat, cat or dog etc.
[^]Represents any character not specified within [].'[^ack]%' represents a value that does not start with a, c or k, for example - bat, dog or rat etc.

Syntax

The syntax for using LIKE and NOT LIKE keywords in T-SQL (Transact-SQL) are given below:

/* Using LIKE keyword */
SELECT column1, column2, ...
FROM table_name
WHERE column LIKE pattern;

/* Using NOT LIKE keyword */
SELECT column1, column2, ...
FROM table_name
WHERE column NOT LIKE pattern;

The table below describes patterns which is used with LIKE keyword and uses (%) and (_).

PatternDescription
'J%'A value that start with "J".
'%n'A value that end with "n".
'%oh%'A value that have "oh" in any position.
'_o%'A value that have "o" in the second position.
'J_%'A value that start with "J" and have at least 2 characters.
'J__%'A value that start with "J" and have at least 3 characters.
'J%n'A value that start with "J" and ends with "n".

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Using the % Wildcard : To select all records of the Employee table with Name starting with 'Jo', the query is given below.

    SELECT * FROM Employee
    WHERE Name LIKE 'Jo%';
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    3JoParis272800
  • Using the % Wildcard with NOT LIKE keyword: NOT LIKE keyword is used as the negation of LIKE keyword. For example, to select all records of the Employee table with Name not starting with 'Jo', the following query can be used:

    SELECT * FROM Employee
    WHERE Name NOT LIKE 'Jo%';
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800
  • Using the _ Wildcard : To select all records of the Employee table with Name containing 'o' as second character, the query is mentioned below.

    SELECT * FROM Employee
    WHERE Name LIKE '_o%';
    

    The result of the above code will be:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    3JoParis272800
  • Using the _ Wildcard with NOT LIKE keyword: To select all records of the Employee table with Name not containing 'o' as second character, the query is given below:.

    SELECT * FROM Employee
    WHERE Name NOT LIKE '_o%';
    

    The result of the above code will be:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800

❮ T-SQL - Keywords