
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Specific Value in MySQL Column
To replace, use the REPLACE() MySQL function. Since you need to update the table for this, use the UPDATE() function with the SET clause.
Following is the syntax −
update yourTableName set yourColumnName=replace(yourColumnName,yourOldValue,yourNewValue);
Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), CountryName varchar(100) ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Bob','AUS'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Chris','US'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David','UK'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Adam','US'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | FirstName | CountryName | +-----------+-------------+ | John | AUS | | Bob | AUS | | Chris | US | | David | UK | | Adam | US | +-----------+-------------+ 5 rows in set (0.00 sec)
Following is the query to replace a specific value −
mysql> update DemoTable set CountryName=replace(CountryName,'AUS','US'); Query OK, 2 rows affected (0.17 sec) Rows matched: 5 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | FirstName | CountryName | +-----------+-------------+ | John | US | | Bob | US | | Chris | US | | David | UK | | Adam | US | +-----------+-------------+ 5 rows in set (0.00 sec)
Advertisements