SQL remove newlines
Flatten multi-line text into one line. The subtlety is that Windows line breaks are two characters, and each engine writes them differently.
- Категория
- Strings
- Returns
string- In this category
- 39 of 172
Синтаксис
REPLACE(s, CHAR(13) + CHAR(10), ' ')
REPLACE(s, CHR(10), ' ')
Each block is a separate form. The support table and the examples show which engine takes which.
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | Supported under this name. |
| PostgreSQL | Да | Supported under this name. |
| MySQL | Да | Supported under this name. |
| SQLite | Да | Supported under this name. |
| Oracle | Да | Supported under this name. |
Примеры
SQL Server
SELECT REPLACE(REPLACE(note, CHAR(13), ''), CHAR(10), ' ') AS flat FROM notes;
PostgreSQL
SELECT REPLACE(note, E'\n', ' ') AS flat FROM notes;
MySQL
SELECT REPLACE(note, '\n', ' ') AS flat FROM notes;
Oracle
SELECT REPLACE(note, CHR(10), ' ') AS flat FROM notes;
Полезно знать
- CRLF is two characters: strip CHAR(13) and CHAR(10) separately or in that order, or Windows-pasted data leaves stray carriage returns.
- Plain TRIM only touches the ends; interior newlines survive every TRIM variant.
- PostgreSQL writes the escape as E'\n' (standard strings treat \ literally); MySQL uses '\n' unless NO_BACKSLASH_ESCAPES is set; CHR(10)/CHAR(10) is the portable spelling.
- Watch for tabs too while you are at it (CHAR(9)/CHR(9)), since pasted data rarely contains only one kind of whitespace.
Questions about Removing newlines
Why do newlines survive my REPLACE?
Usually the data has CRLF and you replaced only LF; the carriage returns remain. Remove CHAR(13) and CHAR(10) as separate passes.
How do I write a newline in a string literal?
Engine-specific: CHAR(13)/CHAR(10) on SQL Server, CHR(10) on Oracle, E'\n' on PostgreSQL, '\n' on MySQL, char(10) on SQLite (which does not process backslash escapes). See the newline-literal page for the full set.
Can I just normalize whitespace instead?
Yes: REGEXP_REPLACE(note, '[[:space:]]+', ' ') collapses newlines, tabs and space runs in one call on MySQL 8+, Oracle and SQL Server 2025. PostgreSQL needs the 'g' flag as a fourth argument or it replaces only the first run. '\s+' works too, except on MySQL, where the string literal swallows the backslash unless you write '\\s+'.
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, SQLite, 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.