Check if text contains a word in SQL
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
| name | category |
|---|---|
| Desk Lamp | Office |
| Standing Desk | Furniture |
| Ergo Chair | Furniture |
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.
WHERE name LIKE '%an%'.
This page on one card
