Course outline

SQLite / Basic: the embedded engine and correct data / Lesson 7

Text comparison and literal pattern searches

Define and test case, collation, and wildcard behavior for labels in a disposable SQLite fixture.

About 4 min readContent reviewed
On this page

Lesson goals

  • Distinguish equality with a collation from LIKE pattern matching.
  • Escape literal wildcard characters and describe the limits of built-in case handling.
  • Design representative comparison tests without assuming a universal Unicode policy.

Start with a comparison contract

A catalog search needs a stated meaning for equality, case, punctuation, whitespace, and ordering. A product code may need exact matching while a human label may need a language-aware search. These are different requirements even when both are stored as TEXT. Write down whether the search is exact or patterned, which scripts must work, and whether two differently written labels are allowed to identify the same item.

SQLite selects a collation for text comparisons according to expression and column rules. Its built-in BINARY, NOCASE, and RTRIM collations have distinct meanings. In particular, built-in NOCASE folds the 26 ASCII uppercase letters; it does not provide complete Unicode case folding. A locale-sensitive product must choose and test an appropriate application or extension strategy rather than infer it from the collation name.

Compare ASCII labels

The following self-contained fixture is for a new disposable database. It does not connect to an existing application or promise a particular order without ORDER BY. Run the setup only once in an empty lab if practicing locally. With the built-in NOCASE collation, the comparison matches the two ASCII spellings of the word pen; the other labels are distinct.

sql Set up labels and compare with NOCASE
CREATE TABLE lab_labels (id INTEGER PRIMARY KEY, label TEXT NOT NULL);
INSERT INTO lab_labels VALUES (1, 'Pen'), (2, 'PEN'), (3, '10% discount'), (4, '10x discount');
SELECT id, label FROM lab_labels WHERE label = 'pen' COLLATE NOCASE ORDER BY id;

The query compares a label to pen under NOCASE and orders matching identifiers. The expected fixture rows are 1 and 2. This example addresses ASCII equality; it does not establish language-specific sorting or Unicode normalization.

Match a literal wildcard

LIKE interprets percent as zero or more characters and underscore as one character. Therefore a literal percent in user input needs escaping when the intended operation is a literal search. Choose an escape character, escape that character itself as well as percent and underscore in the input, and bind the completed pattern as a value in application code. The SQL below shows a fixed pattern for clarity.

sql Treat percent as a literal
SELECT id, label FROM lab_labels
WHERE label LIKE '10!% discount' ESCAPE '!'
ORDER BY id;

The exclamation mark escapes the following percent, so the fixed pattern matches id 3 rather than id 4. ESCAPE supplies one character. This is an illustrative query against the preceding disposable fixture; production values should be bound by the driver.

Separate LIKE from collation

LIKE has its own matching behavior and can be overridden by an application or extension. In SQLiteโ€™s standard implementation its default case folding is limited to ASCII; applying a text collation does not by itself define a complete multilingual LIKE policy. Test the actual deployed build, configured functions, and representative Arabic and accented text. Do not silently change matching rules for an existing unique key without checking collisions and migration consequences.

Verify the application boundary

Create a table of intended input and expected matches covering empty text, literal percent and underscore, the chosen escape character, ASCII case pairs, and relevant non-ASCII labels. Run those checks through the same driver and configuration used by the application. Confirm that value binding prevents SQL structure injection, while wildcard escaping preserves literal search meaning; those are related but separate protections. Review any index or performance decision against the actual query plan and workload.

Common errors and fixes

A literal percent finds many unexpected labels.

Why it happens
The input was placed in a LIKE pattern without escaping its wildcard.
How to fix it
Escape the escape character, percent, and underscore before binding the pattern.
Prevent it next time
Test wildcard and escape characters in literal-search integration cases.

A case-insensitive search misses a non-ASCII case variant.

Why it happens
Built-in ASCII case folding was assumed to implement all Unicode rules.
How to fix it
Choose a documented matching strategy for the supported scripts and verify the deployed build.
Prevent it next time
Include representative multilingual examples in acceptance tests.

A result order changes when labels compare equally.

Why it happens
The query did not specify a unique tie-breaker.
How to fix it
Add a stable identifier to ORDER BY after the intended text ordering.
Prevent it next time
Specify deterministic ordering in the report contract.

2 practice exercises

1. Using the fixture, which rows match the fixed escaped pattern and why?

Show worked solution

Only id 3 matches: !% means a literal percent, while id 4 has x at that position. The surrounding text also matches exactly under the illustrated standard LIKE behavior. Verify against the actual configured implementation before relying on the same rule elsewhere.

2. A user enters 20%_off for a literal search with ! as the escape character. What pattern represents the entire value?

Show worked solution

The pattern is 20!%!_off: prefix percent and underscore with !. Bind this completed string as the pattern and include ESCAPE with ! in the SQL. If the user input itself includes !, double that character during escaping as well.

Key takeaways

  • Equality collations and LIKE pattern matching answer different questions.
  • Built-in NOCASE and default LIKE case behavior do not provide full Unicode case folding.
  • Bind values and escape wildcard characters when literal pattern semantics are required.

Official references