Replacing numbers on a comma delimited result with MySQL?

MySQLMySQLi Database

For this, use CASE statement along with FIND_IN_SET(). Let us first create a table −

mysql> create table DemoTable1629
    -> (
    -> Month varchar(100)
    -> );
Query OK, 0 rows affected (0.64 sec)

Insert some records in the table using insert command.

mysql> insert into DemoTable1629 values('2,4,6');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1629 values('1,3,5,12');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1629 values('7,8,9,10');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1629;

This will produce the following output −

+----------+
| Month    |
+----------+
| 2,4,6    |
| 1,3,5,12 |
| 7,8,9,10 |
+----------+
3 rows in set (0.00 sec)

Following is the query to replace numbers on a comma delimited result −

mysql> select trim(trailing ',' from concat(
    ->     CASE WHEN FIND_IN_SET(1, Month) THEN 'January,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(2, Month) THEN 'Feburary,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(3, Month) THEN 'March,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(4, Month) THEN 'April,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(5, Month) THEN 'May,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(6, Month) THEN 'June,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(7, Month) THEN 'July,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(8, Month) THEN 'August,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(9, Month) THEN 'September,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(10,Month) THEN 'October,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(11,Month) THEN 'November,' ELSE '' END,
    ->     CASE WHEN FIND_IN_SET(12,Month) THEN 'December,' ELSE '' END
    -> ))
    -> from DemoTable1629;

This will produce the following output −

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| trim(trailing ',' from concat(
    CASE WHEN FIND_IN_SET(1, Month) THEN 'January,' ELSE '' END,
    CASE WHEN FIND_IN_SET(2, Month) THEN 'Feburary,' ELSE '' END,
    CASE WHEN FIND_IN_SET(3, Month) THEN 'Marh,' ELSE '' END,
    CASE WHEN FIND_IN_SET(4, Mon |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Feburary,April,June                                                                                                                                                                                                                                                |
| January,March,May,December                                                                                                                                                                                                                                        |
| July,August,September,October                                                                                                                                                                                                                                      |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.04 sec)
raja
Published on 17-Dec-2019 10:57:59
Advertisements