Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I search SQLite text for a pattern with LIKE?

Quick answer

Use LIKE in a WHERE clause. In its pattern, % matches any sequence of characters, including none, while _ matches exactly one character. For example, name LIKE 'Al%' finds names beginning with Al. SQLite’s default LIKE ignores ASCII letter case, but is case-sensitive for Unicode characters beyond ASCII. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How do I find names that begin with “Al” in SQLite? I’m not sure what to put after the letters.

Geez

Put % after the starting text. It stands for any sequence of characters, including an empty sequence, so LIKE 'Al%' matches both Al and longer names that begin with Al. [S1]

Here is a small, runnable example using invented names:

CREATE TABLE people (name TEXT);
INSERT INTO people (name) VALUES
  ('Alice'),
  ('Al'),
  ('Malik'),
  ('Ari');

SELECT name
FROM people
WHERE name LIKE 'Al%'
ORDER BY name;

The matching rows are Al and Alice. Malik contains “Al” but does not begin with it, and Ari has a different beginning. The ORDER BY makes the output order explicit.

Follow-up question

What if I need exactly one character between two letters?

Geez

Use _ for exactly one character. For example, name LIKE 'A_i' requires a name with A, then one character, then i. [S1]

Add one invented row and try it:

INSERT INTO people (name) VALUES ('Ami');

SELECT name
FROM people
WHERE name LIKE 'A_i'
ORDER BY name;

The result is Ami and Ari: each has exactly one character between A and i. A name such as Alii would not match this pattern because it has two characters between those letters. If you need a variable number of characters in that position, use % instead of _.

Follow-up question

Does LIKE care whether the name uses uppercase or lowercase letters?

Geez

By default, SQLite’s LIKE ignores case differences for ASCII letters, so a pattern such as 'Al%' can match an ASCII name beginning with al as well as one beginning with Al. For Unicode characters beyond ASCII, default matching is case-sensitive. The ICU extension provides broader Unicode case folding. [S1]

For a quick check, add ('alex') to the sample table and run:

SELECT name
FROM people
WHERE name LIKE 'Al%'
ORDER BY name;

alex matches under SQLite’s default ASCII case behavior. The documented behavior does not establish that every Unicode case pair will match, nor that the ICU extension is installed in your particular SQLite setup. If your data includes non-ASCII letters and the result matters, test with representative values in the environment where the query will run.

Follow-up question

Can you give me a simple way to build and check my own pattern?

Geez

Start with the part of the text you know, then choose the wildcard for the uncertain part. Keep the example row values beside the query so you can compare the expected matches with the actual output.

Copy and adapt this pattern:

-- Example: find names that start with Jo
SELECT name
FROM people
WHERE name LIKE 'Jo%'
ORDER BY name;

For one required character between known letters, substitute _, as in LIKE 'J_n'. For a beginning match, put % after the known prefix; for a match containing text anywhere, place % on both sides, as in LIKE '%Jo%'. That last pattern matches a substring, not just a prefix. These patterns follow the meanings of % and _ described above. [S1]

A practical check is to run the query against a few rows you can inspect, including one you expect to match and one you expect not to match. If the results differ, check the placement of the wildcards and whether the letters involved are ASCII or Unicode.

A practical checklist

  • Write the known text exactly as it appears in your sample values.

  • Use % where any number of characters, including none, may occur.

  • Use _ where exactly one character may occur.

  • Choose whether the pattern should match a prefix, a substring, or a fixed-length shape.

  • Test the query against representative rows and inspect the returned names.

  • For non-ASCII case matching, verify behavior in your SQLite setup; default behavior is not broad Unicode case folding.

A few more useful answers

Does `Al%` also match the name `Al`?

Yes. % can match an empty sequence, so the pattern can match the prefix by itself as well as longer text. [S1]

How do I find text anywhere in a name rather than only at the start?

Place % before and after the text, such as LIKE '%Jo%'. That allows characters on either side of the substring. [S1]

Will default SQLite LIKE match uppercase and lowercase Unicode letters as equivalent?

Not generally: the documented default is case-insensitive for ASCII letters and case-sensitive for Unicode characters beyond ASCII. The ICU extension provides broader Unicode case folding. [S1]

Sources

References used for this guide.

  1. [S1] SQLite SQL Language Expressions: LIKE patterns and case behavior
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work