English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQLite LIKE Clause

SQLite LIKE operator is used to match text values with patterns using wildcards. If the search expression can match the pattern expression, the LIKE operator will return true, that is1. There are two wildcard characters with the LIKE operator-Used together

  • Percent sign (%)

  • Underscore (_)

The percent sign represents zero, one, or more numbers or characters. The underscore represents a single number or character. These symbols can be used together.

Syntax

The following is the basic syntax of % and _.

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'
or
SELECT FROM table_name WHERE column LIKE '_XXXX_'

You can combine n conditions using AND or OR operators. Here, XXXX can be any numeric or string value.

Online examples

The following table lists many examples showing WHERE parts with different LIKE clauses and with the '%' and '_' operators.

Serial numberDeclarations and descriptions
1

WHERE SALARY LIKE '200%

search for a value starting with2any value starting with 00.

2

WHERE SALARY LIKE '%200%

any value with 00 at any position.2values starting with 00.

3

WHERE SALARY LIKE '_00%'

any value with 00 at the second and third positions.

4

WHERE SALARY LIKE '2_%_%'

search for a value starting with2starting with and at least3any value with any character.

5

WHERE SALARY LIKE '%2'

search for a value starting with2ending with any value

6

WHERE SALARY LIKE '_2%3'

search for the second position with2and3ending with any value

7

WHERE SALARY LIKE '2___3'

search for a value starting with2both beginning and3ending with any five-digit number.

Let's take a real example, considering the COMPANY table with the following records.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

Below is an example that will display any value in the five-digit number at the end of AGE in the COMPANY table.2All records starting with.

sqlite> SELECT * FROM COMPANY WHERE AGE LIKE '2%';

This will produce the following result.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

Below is an example that will display all records in the COMPANY table where the ADDRESS contains a hyphen (-)。

sqlite> SELECT * FROM COMPANY WHERE ADDRESS LIKE '%-%';

This will produce the following result.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0
6           Kim         22          South-Hall  45000.0