Update a column of text with MySQL REPLACE()

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('8565-9848-7474');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('9994-6464-8737');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('6574-9090-7643');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------+
| Code           |
+----------------+
| 8565-9848-7474 |
| 9994-6464-8737 |
| 6574-9090-7643 |
+----------------+
3 rows in set (0.00 sec)

Following is the query to replace column value −

mysql> update DemoTable
set Code=replace(Code,'9994-6464-8737','9994-9999-88888');
Query OK, 1 row affected (0.10 sec)
Rows matched : 3 Changed : 1 Warnings : 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+-----------------+
| Code            |
+-----------------+
| 8565-9848-7474  |
| 9994-9999-88888 |
| 6574-9090-7643  |
+-----------------+
3 rows in set (0.00 sec)
raja
Published on 07-Oct-2019 10:50:48
Advertisements