How to remove all instances of a specific character from a column in MySQL?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> FirstName varchar(100)
   -> );
Query OK, 0 rows affected (0.41 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Adam^^^');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('^^^^^^^^Carol');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('Robert^^^^^^');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| FirstName     |
+---------------+
| Adam^^^       |
| ^^^^^^^^Carol |
| Robert^^^^^^  |
+---------------+
3 rows in set (0.00 sec)

Following is the query to remove all instances of a specific character from a column in MySQL. Here, we are removing all the instances of the special character ^ −

mysql> update DemoTable set FirstName=replace(FirstName,'^','');
Query OK, 3 rows affected (0.20 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Adam      |
| Carol     |
| Robert    |
+-----------+
3 rows in set (0.00 sec)
raja
Published on 11-Jul-2019 16:17:03
Advertisements