// conditional reference

SQL LIKE operator

Pattern matching where percent matches any run of characters and underscore matches exactly one.

カテゴリ
条件付き
Returns
boolean
In this category
25 of 43

構文

col LIKE 'a%' | col NOT LIKE 'a%' | col LIKE 'a%' ESCAPE '\'

エンジンサポート

エンジンSupportメモ
SQL Server はい Supported under this name.
PostgreSQL はい case-sensitive; ILIKE for the insensitive form
MySQL はい Supported under this name.
SQLite はい LIKE folds ASCII case only; PRAGMA case_sensitive_like changes it
Oracle はい case-sensitive unless NLS_COMP=LINGUISTIC with a case-insensitive NLS_SORT

例

Any engine

SELECT name FROM products WHERE name LIKE 'tee-%';

PostgreSQL

SELECT name FROM products WHERE name ILIKE 'tee-%';

知っておくと便利なこと

  • NULL on either side yields UNKNOWN, so NOT LIKE drops NULL rows too, just like an = comparison would.
  • Case sensitivity differs: PostgreSQL and Oracle LIKE are case-sensitive (PostgreSQL has ILIKE), SQLite folds ASCII only ('A' LIKE 'a' is true, 'Ä' LIKE 'ä' is false), MySQL and SQL Server follow the column collation.
  • T-SQL adds bracket classes: LIKE '[0-9]%' matches a leading digit, and LIKE '[%]' matches a literal percent. The other four engines treat brackets as ordinary characters.
  • A leading % defeats B-tree index use; full-text or trigram indexes cover those cases.

Questions about LIKE

How do I make LIKE case-insensitive?

ILIKE in PostgreSQL; SQLite's LIKE already folds ASCII case; MySQL and SQL Server depend on the column collation.

Does LIKE match NULL?

No: NULL LIKE anything is UNKNOWN, and the row is filtered out even with NOT LIKE.

How do I match a literal percent sign?

Use the ESCAPE clause: col LIKE '100!%' ESCAPE '!'.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.