SQL NORMALIZE function
Converts a string to a canonical Unicode normalization form so visually equal text compares equal.
- Categoria
- Strings
- Returns
string- In this category
- 130 of 172
Sintaxe
normalize(string [, form])
forms: NFC (default), NFD, NFKC, NFKD
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No normalize function; normalize in the application or CLR. |
| PostgreSQL | Sim | Built in since PostgreSQL 13; needs a UTF8 database. |
| MySQL | Not built in | No normalize function; some collations compare the forms as equal. |
| SQLite | Not built in | No normalize function; normalize in the application. |
| Oracle | Different name or syntax | COMPOSE gives NFC; DECOMPOSE gives NFD, or NFKD with 'COMPATIBILITY'. |
Exemplos
PostgreSQL
SELECT normalize(U&'cafe\0301', NFC) = 'café' AS same; -- true
PostgreSQL
SELECT count(*) FROM users WHERE display_name IS NOT NFC NORMALIZED;
Bom saber
- 'café' can be stored two ways: the single character é, or e followed by a combining accent. They look identical and compare unequal until both are normalized to the same form.
- NFC composes characters into the fewest code points and is what most systems expect; NFD decomposes them, as older macOS HFS+ file names did. NFKC and NFKD also fold compatibility characters such as ligatures and full-width letters.
- The predicate str IS [NOT] [NFC | NFD | NFKC | NFKD] NORMALIZED tests a value's form without converting it, which is how you find unnormalized rows.
Questions about NORMALIZE
Why do two identical-looking strings compare unequal?
They are probably in different Unicode forms: composed versus decomposed accents. Apply normalize(col, NFC) to both sides, or better, normalize when writing.
Which normalization form should I use?
NFC for almost everything: it is what the web, JSON APIs and most input methods produce. Use NFKC only when you deliberately want compatibility characters folded, for example for search keys.
How do I find rows that are not normalized?
WHERE col IS NOT NFC NORMALIZED (PostgreSQL 13+), then UPDATE ... SET col = normalize(col, NFC).
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, Oracle.
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.