- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 only a specific value from a column in MySQL
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)
- Related Articles
- Get only a single value from a specific MySQL row?
- Replace the empty values from a MySQL table with a specific value
- MySQL query to replace a column value
- Swap a specific column value in MySQL
- Replace array value from a specific position in JavaScript
- Replace a specific duplicate record with a new value in MySQL
- Fetch a specific column value (name) in MySQL
- Updating only a single column value in MySQL?
- Update only a single column value in MySQL
- MySQL query to replace special characters from column value
- Place a specific value for NULL values in a MySQL column
- Select a specific value between two column values in MySQL?
- MySQL insert a value to specific row and column
- How to replace only the first repeated value in a string in MySQL
- How to select distinct value from one MySQL column only?

Advertisements