Replace part of string in MySQL table column?


To replace part of string in MySQL table column, you can use REPLACE(). Following is the syntax −

update yourTableName set yourColumnName = REPLACE(yourColumnName ,'yourOldValue','yourNewValue');

Let us first create a table −

mysql> create table replacePartOfStringDemo
   -> (
   -> WebsiteURL varchar(100)
   -> );
Query OK, 0 rows affected (0.47 sec)

Following is the query to insert records in the table using insert command −

mysql> insert into replacePartOfStringDemo(WebsiteURL) values('www.mysqlQuestion.com');
Query OK, 1 row affected (0.14 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from replacePartOfStringDemo;

This will produce the following output −

+-----------------------+
| WebsiteURL            |
+-----------------------+
| www.mysqlQuestion.com |
+-----------------------+
1 row in set (0.00 sec)

Let us now replace part of a string in MySQL table column. Here, “mysqlQuestion“ is replaced with “mysql” −

mysql> update replacePartOfStringDemo set
WebsiteURL=REPLACE(WebsiteURL,'www.mysqlQuestion.com','www.mysql.com');
Query OK, 1 row affected (0.20 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table record once again −

mysql> select * from replacePartOfStringDemo;

This will produce the following output −

+---------------+
| WebsiteURL    |
+---------------+
| www.mysql.com |
+---------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements