
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to find and replace string in MySQL database for a particular string only?
Use the replace() function to replace string in MySQL Database.
The syntax is as follows
UPDATE yourTableName SET yourColumnName=replace(yourColumnName,'yourExistingValue','yourNewValue') WHERE <yourCondition>>;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table findAndReplaceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentFirstName varchar(20) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into findAndReplaceDemo(StudentFirstName) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('Maxwell'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from findAndReplaceDemo;
The following is the output
+----+------------------+ | Id | StudentFirstName | +----+------------------+ | 1 | Carol | | 2 | David | | 3 | Bob | | 4 | Sam | | 5 | Mike | | 6 | Maxwell | +----+------------------+ 6 rows in set (0.00 sec)
Here is the query to find and replace string in MySQL database for a particular string only
mysql> update findAndReplaceDemo -> set StudentFirstName=replace(StudentFirstName,'Maxwell','Chris') -> where StudentFirstName='Maxwell'; Query OK, 1 row affected (0.16 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again, the value 'Maxwell’ have been changed to ‘Chris.
The query is as follows
mysql> select *from findAndReplaceDemo;
The following is the output with updated value
+----+------------------+ | Id | StudentFirstName | +----+------------------+ | 1 | Carol | | 2 | David | | 3 | Bob | | 4 | Sam | | 5 | Mike | | 6 | Chris | +----+------------------+ 6 rows in set (0.00 sec)
- Related Articles
- How to replace only the first repeated value in a string in MySQL
- MySQL find/ replace string in fields?
- Quickly search for a string in MySQL database?
- C++ Program to find and replace in a string
- Find And Replace in String in C++
- How to replace a particular character in a MySQL column?
- How to find string count of a particular id in a column using a MySQL query?
- How to find a replace a word in a string in C#?
- How can we see only name and types of the stored routines in a particular MySQL database?
- How to replace “and” in a string with “&” in R?
- How can we see only the list of stored procedures in a particular MySQL database?
- How can we see only the list of stored functions in a particular MySQL database?
- How to replace a character in Objective-C String for iPhone SDK?
- How to Replace characters in a Golang string?
- In MySQL how to replace all NULL values in a particular field of a particular table?

Advertisements