bhylu.com

Advertisements

SQL Wildcards

A wildcard stands for part of a text pattern. When the data itself contains a percent sign or underscore, you need a way to distinguish that literal character from the wildcard meaning.

Understand the idea

ESCAPE names a character that introduces a literal wildcard in the pattern. This self-contained example searches a small temporary result rather than changing the shop tables. The exclamation mark tells LIKE to read the following percent sign literally. Different pattern languages have different rules; do not mix LIKE with regular-expression syntax.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

WITH labels(label) AS (
  VALUES ('Save 10%'), ('Save 100'), ('Member offer')
)
SELECT label FROM labels
WHERE label LIKE '%!%%' ESCAPE '!';

Read the result

Only Save 10% is returned because it contains an actual percent character. Save 100 has digits but no literal percent sign.

Try one change

Add a label containing an underscore and adapt the escaped pattern to search for a literal underscore.