
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
Remove Specific Word in a Comma Separated String with MySQL
Let us first create a table −
mysql> create table DemoTable836(FirstName SET('John','Chris','Adam')); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable836 values('John,Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable836 values('John,Chris,Adam'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable836 values('Chris,Adam'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable836 values('John,Adam'); Query OK, 1 row affected (0.37 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable836;
This will produce the following output −
+-----------------+ | FirstName | +-----------------+ | John,Chris | | John,Chris,Adam | | Chris,Adam | | John,Adam | +-----------------+ 4 rows in set (0.00 sec)
Following is the query to remove a specific word in a comma-separated string −
mysql> update DemoTable836 set FirstName = FirstName &~ (1 << FIND_IN_SET('Chris', FirstName) - 1); Query OK, 3 rows affected (0.21 sec) Rows matched: 4 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable836;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | John | | John,Adam | | Adam | | John,Adam | +-----------+ 4 rows in set (0.00 sec)
Advertisements