
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
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