Knowledge base / SQL

Case-insensitive text comparison in SQL: equality and collation

The historical label CASE_INSENSITIVE_COMPARE describes a comparison problem. I show syntax for specific engines and separate literal equality from pattern matching.

Kajo RudzińskiPublished Updated 5 min read

Two streams of similar shapes passing through a comparison gate.

I want to find the label alpha_beta regardless of its letter case. First I establish the engine and comparison rule. This page’s historical name, CASE_INSENSITIVE_COMPARE, describes a problem. It is not a function call used in these examples. Instead of adding parentheses to that label, I choose a collation or a case-converted equality expression appropriate to the particular database.

All data are synthetic. The main example concerns MySQL 8.4; separate fragments use PostgreSQL 18 and SQL Server T-SQL. I do not transfer collation names between engines. International text also requires decisions about language, accents and Unicode normalisation. ASCII examples demonstrate the basic mechanism without pretending to resolve every such case.

MySQL: a compatible character set and collation

The MySQL reference explains that a collation belongs to a particular character set. COLLATE cannot accept an arbitrary name for every expression. I therefore do not apply utf8_general_ci to a utf8mb4 expression. This example defines both explicitly:

CREATE TABLE labels (
    id INT PRIMARY KEY,
    label VARCHAR(50)
      CHARACTER SET utf8mb4
      COLLATE utf8mb4_0900_ai_ci
);

INSERT INTO labels (id, label) VALUES
    (1, 'alpha_beta'),
    (2, 'ALPHA_BETA'),
    (3, 'alphaXbeta'),
    (4, NULL);

SELECT id
FROM labels
WHERE label = _utf8mb4'alpha_beta'
ORDER BY id;

The result contains IDs 1 and 2. Under this collation, case does not distinguish the first two labels. The third replaces the underscore with X, so equality fails. The fourth is missing, and ordinary equality to a text value does not select it. ORDER BY specifies presentation order instead of leaving it to happenstance.

The collation naming conventions identify ci as case-insensitive and ai as accent-insensitive. This collation therefore ignores more than case. If accents must remain distinct, choose a suitable available collation and examine its rules. An example using English labels does not justify dropping that distinction from the requirements of a multilingual application.

For an existing table, inspect the column definition and configuration first. Do not assume every installation has the same defaults. Applying a rule to one expression differs from changing a column definition. A lasting change also needs consideration of grouping, sorting and uniqueness rules. This example demonstrates comparison rather than recommending a broad settings change to an existing database.

PostgreSQL: LOWER with equality

For a small PostgreSQL 18 example, I use lower, described in the string-function reference. It changes case according to the database locale. This query is independent of the MySQL table:

SELECT id
FROM (VALUES
    (1, 'alpha_beta'::text),
    (2, 'ALPHA_BETA'::text),
    (3, 'alphaXbeta'::text),
    (4, NULL::text)
) AS labels(id, label)
WHERE lower(label) = lower('alpha_beta')
ORDER BY id;

Again, the result contains 1 and 2. For these labels, changing case makes the first two spellings equal without turning X into an underscore. This is a deliberate choice for literal text. I do not present lower as a complete normalisation procedure for every Unicode representation. For multilingual data, establish which pairs should compare equal or different and which rules your environment supplies.

ILIKE is still a pattern

The PostgreSQL documentation describes ILIKE as case-insensitive pattern matching under the active locale. An underscore still matches any single character:

SELECT 'alphaXbeta' ILIKE 'alpha_beta' AS pattern_match;
-- true

SELECT lower('alphaXbeta') = lower('alpha_beta') AS equal_text;
-- false

The first condition accepts X at the underscore position. The second compares the actual case-converted strings and retains the difference. This explains why the old ILIKE 'john_doe' example could also match another character after john. To learn more about patterns and literal symbols, read the LIKE entry.

Two sorting gates, one with a fitted opening and another admitting several shapes.

SQL Server: collation on an expression

T-SQL supports COLLATE on a character expression. For example:

SELECT CASE
    WHEN N'alpha_beta' COLLATE Latin1_General_100_CI_AS
       = N'ALPHA_BETA'
    THEN 1 ELSE 0
END AS same_label;

The result is 1. Under SQL Server’s naming rules, CI denotes case insensitivity and AS accent sensitivity. The name is part of the syntax, without string-literal quotes. Do not substitute MySQL’s utf8mb4_0900_ai_ci. A similar role for COLLATE does not imply a shared catalogue of names or identical comparison rules.

Comparing the three dialects

I use this overview when choosing a method. The expressions refer to the data and settings described above; each collation name belongs to its own engine.

Engine Comparison mechanism Equality example Correctness condition
MySQL 8.4 Column collation utf8mb4_0900_ai_ci label = _utf8mb4'alpha_beta' Compatibility with utf8mb4; deliberate accent insensitivity
PostgreSQL 18 lower on both sides of equality lower(label) = lower('alpha_beta') Locale rules and literal text rather than a pattern
SQL Server, T-SQL Expression collation Latin1_General_100_CI_AS label COLLATE Latin1_General_100_CI_AS = N'alpha_beta' An appropriate text type and a collation available in the engine

The table does not claim identical behaviour for every character. It helps identify syntax to examine in a particular environment. I reserve ILIKE for deliberate pattern matching rather than presenting it as a universal equality replacement.

Missing values, indexes and normalisation boundaries

I handle missing labels separately with IS NULL when they belong in the result. I do not automatically replace a missing value with an empty string. Doing so can hide the difference between information never entered and a deliberately empty value. Add both cases to your small table and describe their meaning before deciding how comparison should treat them.

I do not promise that LOWER makes a query faster or that a function prevents every possible index from being used. PostgreSQL documents expression indexes, including indexes on lower(column). Index choice and plan assessment depend on the query and data. This entry addresses comparison correctness, without offering unmeasured performance results.

For an exercise, list which of the four labels should equal alpha_beta. Then add a trailing space, an accent and a percent sign. Avoid assuming identical behaviour across engines. Write the requirement and consult documentation for the chosen collation, type and operator. A deliberate choice starts with deciding which differences may be ignored and which must remain visible.

Key takeaways

Is CASE_INSENSITIVE_COMPARE a function to enter in SQL?

It is this entry's historical label for a problem. In the dialects shown, I use collation or LOWER with equality rather than calling a function with that name.

Does ILIKE replace case-insensitive equality?

ILIKE matches a pattern. An underscore matches any single character, and a percent sign matches a sequence. For literal text, deliberately choose equality or escaping.

Does utf8_general_ci fit a utf8mb4 column?

No. A collation must be compatible with the character set. The MySQL 8.4 example explicitly uses utf8mb4 and utf8mb4_0900_ai_ci.

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.