Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to replace only the first repeated value in a string in MySQL
For this, you can use REGEXP_REPLACE(). Let’s say our string is −
This is my first MySQL query. This is the first tutorial. I am learning for the first time.
We need to replace only the 1st occurrence of a specific word, let’s say “first”. The output should be −
This is my second MySQL query. This is the first tutorial. I am learning for the first time.
Let us create a table −
mysql> create table demo26 −> ( −> value text −> ); Query OK, 0 rows affected (2.04 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo26 values('This is my first MySQL query. This is the first tutorial. I am learning for the first time.');
Query OK, 1 row affected (0.10 sec)
Display records from the table using select statement −
mysql> select *from demo26;
This will produce the following output −
+---------------------------------------------------------------------------------------------+ | value | +---------------------------------------------------------------------------------------------+ | This is my first MySQL query. This is the first tutorial. I am learning for the first time. | +---------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Following is the query to replace only 1st occurrence −
mysql> update demo26 −> set value = REGEXP_REPLACE(value, 'first', 'second', 1, 1 ) ; Query OK, 1 row affected (0.19 sec) Rows matched: 1 Changed: 1 Warnings: 0
Display records from the table using select statement −
mysql> select *from demo26;
This will produce the following output −
+----------------------------------------------------------------------------------------------+ | value | +----------------------------------------------------------------------------------------------+ | This is my second MySQL query. This is the first tutorial. I am learning for the first time. | +----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Advertisements
