SQL Unicode normalization (NFC/NFD)
The same visible text can have different underlying code points. Equality, lengths and hashes all differ until you normalize both sides.
- カテゴリ
- Strings
- Returns
string- In this category
- 78 of 172
構文
normalize(s, NFC)
s IS NFC
Each block is a separate form. The support table and the examples show which engine takes which.
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | はい | normalize() and the IS NFC/NFD/NFKC/NFKD predicates from PG 13 |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Different name or syntax | COMPOSE gives NFC and DECOMPOSE gives NFD; no NFKC/NFKD |
例
PostgreSQL
SELECT length(U&'cafe\0301') AS nfd_len, length('café') AS nfc_len;
-- 5, 4
PostgreSQL
SELECT normalize(U&'cafe\0301', NFC) = 'café' AS same; -- true
知っておくと便利なこと
- NFC composes é as one code point; NFD decomposes it into e plus a combining accent (U+0301). The two forms look identical, compare unequal, and have lengths 5 versus 4 in 'café'.
- PostgreSQL: normalize(s) defaults to NFC; predicates like s IS NFC test without converting. PG 13+.
- Oracle has COMPOSE(s) for NFC and DECOMPOSE(s) for NFD. MySQL, SQL Server and SQLite have no normalize function, so NFD text (common in macOS file names) never equals the NFC form typed on the web.
- Practical fix on those three: normalize in the application on write, or REPLACE the decomposed pairs you know about, incomplete but usually enough.
Questions about Unicode normalization
Why do two identical-looking strings fail equality?
Different normalization forms: one stores the composed character, the other the base letter plus combining accent. Equal-looking, different code points.
Which form should I store?
NFC for almost everything: it is the web's default and the shorter form. Normalize once on write rather than on every comparison.
Does MySQL normalize automatically?
No function does it, but the 0900 collations compare canonically equivalent forms as equal, so 'café' (NFC) = the NFD form under utf8mb4_0900_ai_ci. Lengths, hashes and _bin comparisons still differ, so normalize on write.
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.