MySQL query to get a single value from position of comma-separated string?


For this, use SUBSTRING_INDEX(). Let us first create a table −

mysql> create table DemoTable1615
   -> (
   -> ListOfSubject text
   -> );
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1615 values('Python,Java,MySQL,MongoDB,C,C++,ASP.net');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1615;

This will produce the following output −

+-----------------------------------------+
| ListOfSubject                           |
+-----------------------------------------+
| Python,Java,MySQL,MongoDB,C,C++,ASP.net |
+-----------------------------------------+
1 row in set (0.00 sec)

Here is the query to get a single value from position of comma-separated-string −

mysql> select substring_index(substring_index(ListOfSubject, ',', 4), ',', - 1) as Position from DemoTable1615;

This will produce the following output −

+----------+
| Position |
+----------+
| MongoDB  |
+----------+
1 row in set (0.00 sec)

Updated on: 17-Dec-2019

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements