This SQLite post explains how to use the SQLite LIKE condition to perform pattern matching with syntax and examples.
The SQLite 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 SQLite 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 |
The first SQLite LIKE example that we will look at involves using the % wildcard (percent sign wildcard).
Let's explain how the % wildcard works in the SQLite LIKE condition. We want to find all of the employees whose last_name begins with 'A'.
You can also using the % wildcard multiple times within the same string. For example,
In this SQLite LIKE condition example, we are looking for all employees whose last_name contains the letter 'e'.
Next, let's explain how the _ wildcard (underscore wildcard) works in the SQLite LIKE condition. Remember that _ wildcard is looking for only one character.
For example:
This SQLite LIKE condition example would return all employees whose last_name is 4 characters long, where the first character is 'H' and the last two characters are 'nt'. For example, it could return last_name values such as: 'Hant', 'Hent', 'Hint', 'Hont', 'Hunt', etc.
Next, let's look at how to use the NOT Operator with wildcards.
Let's use the % wilcard with the NOT Operator. You could also use the SQLite LIKE condition to find employees whose department does not start with 'Acc'.
For example:
By placing the NOT Operator in front of the SQLite LIKE condition, you are able to retrieve all employees whose department does not start with 'Acc'.