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;

Output

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;

Output

This will produce the following output −

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

Updated on: 30-Jun-2020

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements