SQL strip non-ASCII characters
Drop or transliterate characters outside the printable ASCII range, the classic cleanup for feeds, keys and legacy interfaces.
- Categoria
- Strings
- Returns
string- In this category
- 58 of 172
Sintaxe
REGEXP_REPLACE(s, '[^ -~]', '')
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sim | REGEXP_REPLACE from SQL Server 2025; TRANSLATE or REPLACE maps before that |
| PostgreSQL | Sim | Supported under this name. |
| MySQL | Sim | Supported under this name. |
| SQLite | Not built in | no REGEXP_REPLACE; nested replace() calls or an extension |
| Oracle | Sim | Supported under this name. |
Exemplos
PostgreSQL
SELECT REGEXP_REPLACE(name, '[^ -~]', '', 'g') AS clean FROM names;
Oracle
SELECT REGEXP_REPLACE(name, '[^ -~]', '') AS clean FROM names;
SQL Server
SELECT TRANSLATE(N'café №5', N'é№', N'e-') AS clean; -- cafe -5
Bom saber
- The range ' -~' (space through tilde) is all printable ASCII: REGEXP_REPLACE(s, '[^ -~]', '') strips accents, emoji, NBSPs and control characters in one pass.
- PostgreSQL, MySQL 8+, Oracle and SQL Server 2025 support that regex (PostgreSQL needs the 'g' flag to replace every match). Older SQL Server needs explicit TRANSLATE(name, N'éàü', N'eau') maps or nested REPLACE.
- Decide strip versus transliterate: stripping 'café' to 'caf' loses data; unaccent-style transliteration keeps it readable. Usually transliterate first, then strip.
Questions about Stripping non-ASCII characters
What does the pattern '[^ -~]' match?
Everything outside printable ASCII: the space character starts the range and tilde ends it. Anything accented, emoji, tab or newline falls outside and gets removed.
How do I keep accented letters but fold them to ASCII?
Transliterate before stripping: PostgreSQL's unaccent extension, TRANSLATE maps with your known character set, or Unicode NFKD normalization plus a combining-mark removal.
Why does SQL Server make this hard?
Before SQL Server 2025, T-SQL had no regex. Build an explicit TRANSLATE/REPLACE map of the characters you actually see, and keep the map strings N'': a varchar literal turns № into ? before TRANSLATE ever sees it.
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: SQL Server, PostgreSQL, MySQL, 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.