In this post explains how to use the LIKE condition in SQL Server (Transact-SQL) to perform pattern matching with syntax and examples.
The SQL Server (Transact-SQL) LIKE condition allows wildcards to be used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement. This allows you to perform pattern matching.
The syntax for the LIKE condition in SQL Server (Transact-SQL) is:
A character expression such as a column or field.
A character expression that contains pattern matching. The patterns that you can choose from are:
Wildcard | Explanation |
---|---|
% | Allows you to match any string of any length (including zero length) |
_ | Allows you to match on a single character |
[ ] | Allows you to match on any character in the [ ] brackets (for example, [abc] would match on a, b, or c characters) |
[^] | Allows you to match on any character not in the [^] brackets (for example, [^abc] would match on any character that is not a, b, or c characters) |
Optional. It allows you to test for literal instances of a wildcard character such as % or _.
The first SQL Server LIKE example that we will look at involves using the % wildcard (percent sign wildcard).
Let's explain how the % wildcard works in the SQL Server LIKE condition. We want to find all of the employees whose last_name begins with 'B'.
For example:
You can also using the % wildcard multiple times within the same string. For example,
In this SQL Server LIKE condition example, we are looking for all employees whose last_name contains the letter 'o'.
Next, let's explain how the _ wildcard (underscore wildcard) works in the SQL Server LIKE condition. Remember that _ wildcard is looking for only one character.
For example:
This SQL Server LIKE condition example would return all employees whose first_name is 4 characters long, where the first two characters is 'Ad' and the last character is 'm'. For example, it could return employees whose first_name is 'Adam', 'Adem', 'Adim', 'Adom', 'Adum', etc.
Here is another example:
You might find that you are looking for an employee_number, but you only have 3 of the 4 digits. The example above, would retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return employees whose employee numbers are:
1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239
Next, let's explain how the [ ] wildcard (square brackets wildcard) works in the SQL Server LIKE condition. Remember that what is contained within the square brackets are characters that you are trying to match on.
For example:
This SQL Server LIKE condition example would return all employees whose first_name is 5 characters long, where the first two characters is 'Sm' and the last two characters is 'th', and the third character is either 'i' or 'y'. So in this case, it would match on either 'Smith' or 'Smyth'.
Next, let's explain how the [^] wildcard (square brackets with ^ wildcard) works in the SQL Server LIKE condition. Remember that what is contained within the square brackets are characters that you do NOT want to match on.
For example:
This SQL Server LIKE condition example would return all employees whose first_name is 5 characters long, where the first two characters is 'Sm' and the last two characters is 'th', and the third character is neither 'i' or 'y'. So in this case, it would match on values such as 'Smath', 'Smeth', 'Smoth', etc. But it would not match on either 'Smith' or 'Smyth'.
Next, let's look at how you would use the SQL Server NOT Operator with wildcards.
Let's use the % wilcard with the NOT Operator. You could also use the SQL Server LIKE condition to find employees whose last_name does not start with 'B'.
For example:
By placing the NOT Operator in front of the SQL Server LIKE condition, you are able to retrieve all employees whose last_name does not start with 'B'.
It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in SQL Server.
Let's say you wanted to search for a % or a _ character in the SQL Server LIKE condition. You can do this using an Escape character.
Please note that you can only define an escape character as a single character (length of 1).
For example:
This SQL Server LIKE condition example identifies the ! character as an escape character. This statement will return all employees whose secret_hint is 123%455.
Here is another more complicated example using escape characters in the SQL Server LIKE condition.
This SQL Server LIKE condition example returns all employees whose secret_hint starts with H and ends in %. For example, it would return a value such as 'Help%'.
You can also use the escape character with the _ character in the SQL Server LIKE condition.
For example:
This SQL Server LIKE condition example returns all employees whose secret_hint starts with H and ends in _. For example, it would return a value such as 'Help_'.