MySQL query to replace only the backslashes from folder path?

MySQLMySQLi Database

To replace backslashes with any other special character, use the REPLACE() method. Let us first create a table −

mysql> create table DemoTable827(Path text);
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable827 values('C\\MySQL');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable827 values('D\\NewFolder\\');
Query OK, 1 row affected (0.47 sec)
mysql> insert into DemoTable827 values('E:\\myFolder\\MyDocument');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable827;

This will produce the following output −

+------------------------+
| Path                   |
+------------------------+
| C\MySQL                |
| D\NewFolder\           |
| E:\myFolder\MyDocument |
+------------------------+
3 rows in set (0.00 sec)

Following is the query to replace only the backslash from the folder path with another special character −

mysql> update DemoTable827 set Path=REPLACE(Path, '\\', '----');
Query OK, 3 rows affected (0.18 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable827;

This will produce the following output −

+------------------------------+
| Path                         |
+------------------------------+
| C----MySQL                   |
| D----NewFolder----           |
| E:----myFolder----MyDocument |
+------------------------------+
3 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 12:21:28
Advertisements