Do Partial Matches Using LIKE
In this lesson you are going to explore how to do pattern matching with the SQL Where clause LIKE operator. Using this phrase allows us perform partial matches of data values and obtain answers to questions which can’t be done with conventional comparisons.
The lesson’s objectives are to:
- learn about the like clause
- understand wild cards
Once you have gone through the lesson, please be sure to do the exercises. These and the examples that follow are based on the sample database. Be sure to get a copy so you can follow along.
The LIKE match condition is used to match values fitting a specified pattern. Unlike the equals (=) comparison operator, which requires an exact match, with the like clause we can specify a pattern to partially match fields. An example where clause using the LIKE condition to find all Employees whose first names start with “R” is:
Where FirstName LIKE 'R%'
You may be wondering what is so special about that search as you could just as easily as written
Where FirstName >= 'R' AND FirstName < 'S'
to achieve the same result, but what about finding all names ending in the letter s? There is no easy way to use the traditional comparison operators to do this, but it’s easily handled with the LIKE operator
Where FirstName LIKE '%s'
The ‘R%’ and ‘%s’ are patterns. Patterns are created using placeholder characters. There are several special characters used:
|%||Match zero or more characters|
|_||Match exactly one character|
For instance the pattern ‘%and%’ would match word ‘Wand’, ‘and’, or ‘Standard.’ To find all state abbreviations starting with the letter N, we could use the pattern ‘N%’ As this would match all values who first character is “N” and then any characters afterwards.
Yet, since state abbreviations are two characters ‘N_’ is more accurate, as this states to first match ‘N’ and then one and only one character thereafter. We can also match an anti-pattern using NOT. If you’re looking for all names that do not end in S, the clause to use is
Where FirstName NOT LIKE '%s' This would match 'Baker', 'Michigan', or 'Wolverine,' but not 'Sales' or 'Kites'
As with other clauses, LIKE operator can be combined with other comparisons using AND and OR. So to, find all employees in New York whose name doesn’t end in S we would write
Where FirstName LIKE '%a%' AND EmployeeID >= 3
Matching a 1-800 Phone Number
So if you wanted to search for a “1-800” phone numbers you could do a search like
PhoneNumber LIKE '%800%'
But that could match more than you bargained for as numbers such as 1-248-703-9800 could also match. So, you could refine the search to be more specific.
PhoneNumber LIKE '%(800)%' OR PhoneNumber LIKE '%800 %'
To match numbers such as (800) 555-1212 or 1-800 555-1212; however, this could backfire, as now numbers such as 1-800-555-1212 wouldn’t match, of course you could catch this with additional match terms. The final result would be:
PhoneNumber LIKE '%(800)%' OR PhoneNumber LIKE '%800 %' OR PhoneNumber LIKE '%800-%'
Matching a Social Security Number
You may have a situation where you wish to retrieve all government ID’s matching the pattern of a US social security ID. In order to do this match you could use the following
WHERE GovernmentID LIKE '___-__-____'
This would match numbers such as ‘123-12-1234′, but not ’12-12-12’ If you were looking to find all ID’s that didn’t match a social security number you could write your query as:
WHERE GovernmentID NOT LIKE '___-__-____'
Exercises using Like Operator
It’s important to practice! Use the sample database to answer these questions.
- Select all customers whose business is on a drive.
- Select all orders shipped in April (Hint: Dates are in YYYY-MM-DD format).
- How many orders were ordered in 2012 with a freight charge less than $7.00?
Answers to Exercises
Congratulations! You just learned how to use the LIKE clause to create sophisticated filtering conditions. More tutorials are to follow! Remember! I want to remind you all that if you have other questions you want answered, then post a comment or tweet me. I’m here to help you. What other topics would you like to know more about?