
- 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
MySQL find/ replace string in fields?
To find/replace string in fields, the syntax is as follows −
update yourTableName set yourColumnName =REPLACE(yourColumnName,yourOldValue,yourNewValue);
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table FindReplaceDemo -> ( -> FileId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FileDirectory text -> ); Query OK, 0 rows affected (0.92 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into FindReplaceDemo(FileDirectory) values('C://User//MySQL'); Query OK, 1 row affected (0.19 sec) mysql> insert into FindReplaceDemo(FileDirectory) values('D://WebsiteImage//image1.jpg'); Query OK, 1 row affected (0.12 sec) mysql> insert into FindReplaceDemo(FileDirectory) values('E://Java//AdvancedJava'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from FindReplaceDemo;
Here is the output −
+--------+------------------------------+ | FileId | FileDirectory | +--------+------------------------------+ | 1 | C://User//MySQL | | 2 | D://WebsiteImage//image1.jpg | | 3 | E://Java//AdvancedJava | +--------+------------------------------+ 3 rows in set (0.00 sec)
Here is the query to find/replace string in fields −
mysql> update FindReplaceDemo -> set FileDirectory =REPLACE(FileDirectory,'E://Java//AdvancedJava','E://Java//SpringAndHibernateFramework'); Query OK, 1 row affected (0.12 sec) Rows matched: 3 Changed: 1 Warnings: 0
Now let us check the table records once again. The query is as follows −
mysql> select *from FindReplaceDemo;
The output is as follows with replaced values −
+--------+---------------------------------------+ | FileId | FileDirectory | +--------+---------------------------------------+ | 1 | C://User//MySQL | | 2 | D://WebsiteImage//image1.jpg | | 3 | E://Java//SpringAndHibernateFramework | +--------+---------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to find and replace string in MySQL database for a particular string only?
- Replace part of string in MySQL table column?
- Find And Replace in String in C++
- Query in MySQL for string fields with a specific length?
- MySQL query to replace part of string before dot
- C++ Program to find and replace in a string
- How to apply Substring() for fields in MySQL to get part of string?
- Substring() for Fields in MySQL?
- How to replace only the first repeated value in a string in MySQL
- Find and replace a part of URL records in MySQL?
- Can we replace a number with a String in a MySQL result set?
- How to concatenate fields in MySQL?
- Replace Character in a String in Java without using replace() method
- How to find a replace a word in a string in C#?
- Find and Replace text in the entire table using a MySQL?

Advertisements