SQLite Like clause
SQLite's LIKE operator is used to match text values in patterns specified by wildcards. The LIKE operator returns true, which is 1, if the search expression matches the pattern expression. There are two wildcard characters used with the LIKE operator:
Percent sign (%)
Underscore (_)
The percent sign (%) represents zero, one or more numbers or characters. An underscore (_) represents a single number or character. These symbols can be used in combination.
Syntax
% The basic syntax of _ is as follows:
WHERE column LIKE 'XXXX%'
or
SELECT FROM table_name
WHERE column LIKE '%XXXX%'
or
SELECT FROM table_name
WHERE column LIKE 'XXXX_'
or
SELECT FROM table_name
WHERE column LIKE '_XXXX'
or
SELECT FROM table_name
WHERE column LIKE '_XXXX_'
You can use the AND or OR operator to combine N quantities of conditions. Here, XXXX can be any number or string value.
Examples
The following examples demonstrate the differences in LIKE clauses with '%' and '_' operators:
Statement | Description |
---|---|
Find any value starting with 200 | |
Find any value containing 200 at any position | |
Find any value whose second and third digits are 00 | |
Look for any value that starts with 2 and has a length of at least Any value of 3 characters | |
Find any value ending with 2 | |
Find any value whose second digit is 2 and ends with 3 | |
Find any value that is 5 digits long and starts with 2 and ends with 3 |