MySQL query to replace backslash from a varchar column with preceding backslash string values

Let us first create a table −

mysql> create table DemoTable
(
   Title varchar(100)
);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('"MySQL');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('MongoDB"');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('"Java"');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('"C"');
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 −

+-----------+
| Title     |
+-----------+
| "MySQL   |
| MongoDB" |
| "Java"  |
| "C"      |
+-----------+
4 rows in set (0.00 sec)

Following is the query to replace backslash from a varchar column with preceding backslash string values −

mysql> update DemoTable set Title=replace(Title,'"','"');
Query OK, 4 rows affected (0.14 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Title    |
+----------+
| "MySQL   |
| MongoDB" |
| "Java"   |
| "C"      |
+----------+
4 rows in set (0.00 sec)
Updated on: 2019-09-26T08:22:18+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements