MySQL update query to remove spaces between letters?


If you have spaces between letters then you can use REPLACE() function to remove spaces.

The syntax is as follows −

UPDATE yourTableName SET yourColumnName=REPLACE(yourColumnName,’ ‘,’’);

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table removeSpaceDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> UserId varchar(20),
   -> UserName varchar(10),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.81 sec)

Now insert some records in the table using insert command. The query is as follows −

mysql> insert into removeSpaceDemo(UserId,UserName) values(' John 12 67 ','John');
Query OK, 1 row affected (0.33 sec)
mysql> insert into removeSpaceDemo(UserId,UserName) values('Carol 23 ','Carol');
Query OK, 1 row affected (0.34 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from removeSpaceDemo;

The following is the output −

+----+------------------+----------+
| Id | UserId           | UserName |
+----+------------------+----------+
|  1 | John 12 67       | John     |
|  2 | Carol 23         | Carol    |
+----+------------------+----------+
2 rows in set (0.00 sec)

Look at the above sample output, we have spaces in between letters.

Now use REPLACE() to remove spaces between letters. The query is as follows −

mysql> update removeSpaceDemo set UserId=REPLACE(UserId,' ','');
Query OK, 2 rows affected (0.63 sec)
Rows matched: 2 Changed: 2 Warnings:

Check the table records once again. The query is as follows −

mysql> select *from removeSpaceDemo;

The following is the output −

+----+----------+----------+
| Id | UserId   | UserName |
+----+----------+----------+
|  1 | John1267 | John     |
|  2 | Carol23  | Carol    |
+----+----------+----------+
2 rows in set (0.00 sec)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements