// conditional reference

SQLite GLOB operator

'file7.txt' GLOB 'file[0-9].txt' is 1. GLOB is SQLite's case-sensitive pattern match, with shell wildcards instead of % and _.

Категория
Условные конструкции
Returns
integer (1, 0 or NULL)
In this category
43 of 43

Синтаксис

expression GLOB pattern

Поддержка движка

ДвижокSupportЗаметки
SQL Server Different name or syntax LIKE supports [0-9] classes; add a _CS_ collation for case
PostgreSQL Different name or syntax LIKE (case-sensitive) or ~ for character classes
MySQL Different name or syntax REGEXP, or LIKE under a _bin collation
SQLite Да GLOB
Oracle Different name or syntax LIKE (case-sensitive) or REGEXP_LIKE

Примеры

SQLite

SELECT 'Apple' GLOB 'A*' AS a, 'Apple' GLOB 'a*' AS b, 'abc' GLOB '?b?' AS c;  -- 1, 0, 1

SQLite

SELECT name FROM (SELECT 'file7.txt' AS name UNION ALL SELECT 'fileX.txt')
WHERE name GLOB 'file[0-9].txt';  -- file7.txt

SQLite

SELECT '12.5' GLOB '*[^0-9.]*' AS a, '12a' GLOB '*[^0-9.]*' AS b;  -- 0, 1

Полезно знать

  • GLOB is case-sensitive: 'Apple' GLOB 'a*' is 0 while 'Apple' LIKE 'a%' is 1.
  • Negated classes use ^: '12a' GLOB '*[^0-9.]*' is 1, which makes GLOB a quick test for unexpected characters.
  • There is no ESCAPE clause. Match a literal * or ? with a one-character class: [*] or [?].
  • GLOB 'abc*' can use an index on a column with the default BINARY collation. LIKE 'abc%' needs a NOCASE column or case_sensitive_like for that.

Questions about GLOB

GLOB or LIKE?

LIKE for case-insensitive ASCII matching with % and _. GLOB for case-sensitive matching and character classes.

Does SQLite support REGEXP?

The grammar has a REGEXP operator, but the core library ships no function behind it. The sqlite3 command-line shell loads one; applications must register their own or load an extension.

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.