mysqldude

Removing linefeeds from text in a database

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
REPLACE(string, "\n", "") (original)
original post
✓✓✓✓✓✓✓✓✓
REPLACE CRLF, then LF, then CR (handles mixed files)
modern rewrite
✓✓✓✓✓✓✓✓✓
REGEXP_REPLACE(string, '[\r\n]+', '') (MySQL 8.0+, MariaDB 10.0.5+)
modern rewrite
✗ 1305✗ 1305✓✓✓✓✓✓✓
REPLACE(string, "\n", "") (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
REPLACE CRLF, then LF, then CR (handles mixed files) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
REGEXP_REPLACE(string, '[\r\n]+', '') (MySQL 8.0+, MariaDB 10.0.5+) modern rewrite
MySQL 5.6✗ 13055.7✗ 13058.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • REGEXP_REPLACE(string, '[\r\n]+', '') (MySQL 8.0+, MariaDB 10.0.5+) (MySQL 5.6, MySQL 5.7): error 1305: FUNCTION t_2.REGEXP_REPLACE does not exist

The original article

First published 2012-04-20. The text below is the original; the table above shows how it behaves on current versions.

Imagine you have imported data into the database and then find out you have also imported the line feeds and want to remove these. This can be done using an UPDATE statement with the REPLACE function:

In our example here we have a table called “enter” with a single field “string”. The “string” field contains some text:

mysql> select * from enter;
+-------------------+
| string            |
+-------------------+
| This is a test 1
 |
| This is a test 2
 |
+-------------------+
2 rows in set (0.00 sec)

Now we need to issue the UPDATE statement using the REPLACE function. In MySQL the set of characters “\n” represents the linefeed character. Carriage return is represented by the “\r”.

mysql> update enter set string = replace(string, "\n", "");
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from enter;
+------------------+
| string           |
+------------------+
| This is a test 1 |
| This is a test 2 |
+------------------+
2 rows in set (0.00 sec)

This function could have worked equally well for DOS type text where both the carriage return and line feed are present by changing the REPLACE to include the “\r” in addition to the linefeed character as follows:

UPDATE enter SET string = REPLACE(string, "\r\n", "");

The order in which the carriage return line feed characters appear in the REPLACE function is important as it will search for all entries having carriage return line feed and not line feed carriage return.