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)
Updated on: 2019-09-03T12:54:08+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements