Knowledge base / SQL

SQL LIKE: wildcards, literal characters and dialect differences

I trace small matching examples and explain why SQL Server bracket classes are not universal LIKE syntax.

Kajo RudzińskiPublished Updated 4 min read

A gate sorts blocks according to a matching shape.

LIKE checks whether text matches a pattern. I start with two wildcards: % means any sequence of characters, including an empty sequence, while _ means exactly one character. Only then do I add the rules of a specific engine. Similar syntax does not guarantee identical handling of case, spaces or additional symbols across databases.

What the pattern actually describes

A LIKE pattern covers the entire string. To find a fragment anywhere inside it, put % at both ends. %sql% finds the substring sql, rather than only a separate word. Under ordinary character comparison, it can match nosql too. I do not treat that construction as automatic identification of a document’s subject.

Pattern Meaning
ab% Text starting with ab, including ab itself
%ab Text ending with ab
%ab% Text containing the fragment ab
ab_ ab followed by exactly one character
J____n J, four arbitrary characters, then n

The last pattern contains six positions in total. An underscore does not mean a digit: in 32-___, it can match a letter as well. Counting positions alone cannot validate a code format. More specific pattern conditions require other constructions, introduced in the REGEXP entry.

A small PostgreSQL example

The examples assume PostgreSQL with deterministic, case-sensitive comparison. All data is synthetic. The LIKE documentation explains the wildcards and whole-string matching.

SELECT name
FROM (VALUES ('ab'), ('abc'), ('ab12'), ('cab')) AS t(name)
WHERE name LIKE 'ab_'
ORDER BY name;

Only abc matches. ab lacks a third character, ab12 has one too many, and cab starts differently. With ab%, the matching values would be ab, abc and ab12. The difference follows from the number of characters permitted after the prefix, rather than table size or query planning.

Match literal percent and underscore characters

If a filename must begin with literal backup_, distinguish the underscore from a wildcard. I choose ! as an explicit escape character. This avoids an extra layer of backslash interpretation inside SQL string literals.

SELECT file_name
FROM (VALUES
    ('backup_2026'),
    ('backupX2026'),
    ('backup_')
) AS f(file_name)
WHERE file_name LIKE 'backup!_%' ESCAPE '!'
ORDER BY file_name;

In this PostgreSQL example, backup_ and backup_2026 match. backupX2026 fails because the character after backup must be an underscore. The final % allows an empty remainder. Similarly, !% denotes a literal percent sign with this escape setting. If the escape character itself is part of the search value, it also needs appropriate handling.

MySQL and SQL Server document ESCAPE too, but I do not assume support in every related product. Microsoft identifies Azure Synapse Analytics as an exception. In MySQL 8.4, backslash handling also depends on SQL mode. This is why I avoid copying the old example with a single backslash without considering the literal parsing rules.

A frame with one fixed opening beside an expandable channel for varying numbers of blocks.

Square brackets belong to a specific dialect

SQL Server T-SQL supports LIKE '[JM]%' for a J or M prefix, and negated classes such as [^J]. This is a Microsoft documented extension. Square brackets are not universal LIKE character classes in PostgreSQL or MySQL.

Two alternative prefixes can be expressed with two conditions:

WHERE name LIKE 'J%' OR name LIKE 'M%'

This is a condition fragment for a name column, rather than a complete query. When combining it with another condition, I enclose the alternatives in parentheses. Character ranges and comparisons can depend on collation, so I do not generalise this small letter example into complete alphabet validation.

Case, spaces and missing values

SQL Server and MySQL character comparisons depend on the expression’s collation. With ordinary deterministic collations, PostgreSQL LIKE distinguishes case, while ILIKE supports case-insensitive matching according to the active locale. Modern PostgreSQL also supports LIKE with nondeterministic collations, including case-insensitive ones. It would therefore be inaccurate to claim that PostgreSQL LIKE always behaves identically regardless of settings.

I do not assume spaces are irrelevant either. Types such as char and varchar, together with engine rules, can affect trailing space comparisons. The examples above deliberately avoid that issue. When real input contains extra spaces, I investigate their source and the data type documentation first. Removing them should be a separate decision about the data, rather than an unexplained side effect of matching.

NULL LIKE 'ab%' is unknown rather than true or false. NOT LIKE does not select that row either. To include missing values, state the rule explicitly, for example name NOT LIKE 'ab%' OR name IS NULL. Replacing missing data with empty text merely to increase the number of returned records changes the meaning of the condition.

Choose the simplest suitable condition

For one specific value, I use equality; for a list of known values, IN; for a prefix or substring, LIKE. Searching linguistic words may call for full-text search, but that is not a replacement for every character matching rule. Nor do I promise that adding an index accelerates any pattern. Performance needs assessment through the particular query plan and environment.

For practice, decide which of ab, abc, xab and ab_ match ab%, then which match ab_. Next, make the underscore literal using ESCAPE. Explaining the effect of one changed character provides a useful foundation for building a more complicated condition.

Key takeaways

Does an underscore in LIKE mean a digit?

No. It matches one arbitrary character, so a length pattern does not establish a numeric format.

Do bracket classes work in every LIKE implementation?

No. Classes such as [JM] are a SQL Server T-SQL extension rather than universal LIKE syntax.

Does NOT LIKE select NULL?

No. A match involving NULL is unknown. Include an explicit IS NULL condition when missing values should be retained.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.