Home > Database > Mysql Tutorial > body text

Perform a search/replace on only the first occurrence of a session variable in MySQL

王林
Release: 2023-09-25 12:01:02
forward
1239 people have browsed it

仅对 MySQL 中会话变量第一次出现的字符执行搜索/替换

To perform a search/replace on only the first occurrence, use the CONCAT and REPLACE() functions.

Query as follows to set user-defined session variables -

mysql> set @Sentence='Thks ks is a my string';
Query OK, 0 rows affected (0.00 sec)
Copy after login

Here, k will be replaced by i only once. The query is as follows. We also used INSTR() -

mysql> select @Sentence as NewString ,CONCAT(REPLACE(LEFT(@Sentence,
INSTR(@Sentence, 'k')), 'k', 'i'),
   -> SUBSTRING(@Sentence, INSTR(@Sentence, 'k') + 1)) as ChangeOnlyOneTime;
Copy after login

The following is the output showing only the first occurrence of the character being replaced -

+------------------------+------------------------+
| NewString              | ChangeOnlyOneTime      |
+------------------------+------------------------+
| Thks ks is a my string | This ks is a my string |
+------------------------+------------------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of Perform a search/replace on only the first occurrence of a session variable in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!