SQL TO_ASCII function
Strips accents from text in a single-byte Latin encoding, turning é into e. It does not work on UTF-8 text.
- Catégorie
- Strings
- Returns
string- In this category
- 136 of 172
Syntaxe
to_ascii(string [, encoding])
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Not built in | No to_ascii; COLLATE ... _AI comparisons, or the application. |
| PostgreSQL | Oui | Supports only LATIN1, LATIN2, LATIN9 and WIN1250. |
| MySQL | Not built in | No to_ascii; accent-insensitive collations handle comparisons. |
| SQLite | Not built in | No to_ascii; transliterate in the application. |
| Oracle | Not built in | No to_ascii; ASCIISTR escapes instead of transliterating. |
Exemples
PostgreSQL
SELECT to_ascii('Karel'); -- works only in a LATIN1, LATIN2, LATIN9 or WIN1250 database
PostgreSQL
CREATE EXTENSION IF NOT EXISTS unaccent;
SELECT unaccent('José Müller') AS plain; -- Jose Muller
Bon à savoir
- to_ascii only reads LATIN1, LATIN2, LATIN9 and WIN1250 text. In a UTF8 database, the usual case, to_ascii(text) fails with 'encoding conversion from UTF8 to ASCII not supported'.
- The encoding argument does not convert anything; it tells to_ascii how to read the bytes. Passing 'LATIN1' for UTF-8 text returns garbage: to_ascii('über', 'LATIN1') comes back as 'A ber'.
- In a UTF8 database use the unaccent extension: unaccent('José Müller') is 'Jose Muller', and its rules file can be extended.
Questions about TO_ASCII
Why does to_ascii fail on my UTF-8 database?
It has no UTF-8 support at all, and naming a Latin encoding only makes it misread the bytes. Use unaccent() from the unaccent extension instead.
What should I use for accent-insensitive search?
unaccent() on both sides in PostgreSQL (an expression index needs an IMMUTABLE wrapper function), an accent-insensitive collation in MySQL or SQL Server, or a pre-computed folded column.
Is to_ascii the same as ASCIISTR?
No. Oracle's ASCIISTR replaces non-ASCII characters with backslash escapes and keeps all the information; to_ascii drops the accents to produce readable plain text.
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.
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.