SQL reference

Check if text contains a word in SQL

In Excel, you'd reach for
fx=ISNUMBER(SEARCH("word", A2)), or a "Contains" text filter

LIKE matches a pattern. % stands for "any text, including none", so '%desk%' means "contains desk", 'desk%' means "starts with" and '%desk' means "ends with". _ stands for exactly one character.

Syntax

WHERE column LIKE '%word%'
Try itEdit the query, then Run
namecategory
Desk LampOffice
Standing DeskFurniture
Ergo ChairFurniture

3 rows

Is it case-sensitive? Depends on the database: SQLite and MySQL usually ignore case for LIKE; Postgres doesn't (use ILIKE there). To be explicit anywhere: WHERE LOWER(name) LIKE '%desk%'.

To get the position of the word instead of yes/no, use the find function: INSTR(text, 'x') (SQLite, MySQL, Oracle), CHARINDEX('x', text) (SQL Server), POSITION('x' IN text) (Postgres). 0 means not found.

Your turn

Find customers whose name contains an anywhere. Show their names.

This page on one card

Check if text contains a word in SQL: the Excel way (=ISNUMBER(SEARCH("word") and the SQL query, side by side
Check if text contains a word in SQL, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows