How to remove hyphens using MySQL UPDATE?


To remove hyphens using MySQL update, you can use replace() function. 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 removeHyphensDemo
   -> (
   -> userId varchar(100)
   -> );
Query OK, 0 rows affected (0.62 sec)

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

mysql> insert into removeHyphensDemo values('John-123-456');
Query OK, 1 row affected (0.22 sec)
mysql> insert into removeHyphensDemo values('Carol-9999-7777-66555');
Query OK, 1 row affected (0.19 sec)
mysql> insert into removeHyphensDemo values('123456-Bob-8765');
Query OK, 1 row affected (0.14 sec)
mysql> insert into removeHyphensDemo values('1678-9870-Sam');
Query OK, 1 row affected (0.21 sec)

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

mysql> select *from removeHyphensDemo;

Here is the output −

+-----------------------+
| userId                |
+-----------------------+
| John-123-456          |
| Carol-9999-7777-66555 |
| 123456-Bob-8765       |
| 1678-9870-Sam         |
+-----------------------+
4 rows in set (0.00 sec)

Here is the query to remove hyphens −

mysql> update removeHyphensDemo
   -> set userId=replace(userId,'-','');
Query OK, 4 rows affected (0.26 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check the table records once again. The query is as follows −

mysql> select *from removeHyphensDemo;

Here is the output without hyphens −

+--------------------+
| userId             |
+--------------------+
| John123456         |
| Carol9999777766555 |
| 123456Bob8765      |
| 16789870Sam        |
+--------------------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

610 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements