Remove new line characters from rows in MySQL?


The Trim() function is used to remove new line characters from data rows in MySQL. Let us see an example. First, we will create a table. The CREATE command is used to create a table.

mysql> create table tblDemotrail
- > (
- > id int,
- > name varchar(100)
- > );
Query OK, 0 rows affected (0.57 sec)

Let us now insert some records.

mysql> insert into tblDemotrail values(1,'John ');
Query OK, 1 row affected (0.15 sec)

mysql> insert into tblDemotrail values(2,' Carol');
Query OK, 1 row affected (0.32 sec)

mysql> insert into tblDemotrail values(3,' Sam ');
Query OK, 1 row affected (0.17 sec)

mysql> insert into tblDemotrail values(4,' Tom
'); Query OK, 1 row affected (0.18 sec) mysql> insert into tblDemotrail values(5,'
Tim '); Query OK, 1 row affected (0.12 sec)

Let us display all the records.

mysql> select *from tblDemotrail;

The following is the output that doesn’t look like normal record, since we included newline characters while adding records.

+------+------------------+
| id   | name             |
+------+------------------+
| 1    |John              |
| 2    | Carol            |
| 3    | Sam              |
| 4    | Tom              |
| 5    |Tim               |
+------+------------------+
5 rows in set (0.00 sec)

To remove new line characters, you need to use the following query.

mysql> update tblDemotrail SET name = TRIM(TRAILING '
' FROM name); Query OK, 0 rows affected (0.00 sec) Rows matched: 5 Changed: 0 Warnings: 0

Updated on: 26-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements