I am trying to split comma-separated (,) values into multiple columns from a string
Sample data:
COL1 COL2 COL3
000002,000003,000042 09,31,51 007,004,007
Expected output:
Pno Cno Sno
000002 09 007
000003 31 004
000042 51 007
I have tried the following query:
SELECT SUBSTRING_INDEX(COL1, ',', 1) Pno
,SUBSTRING_INDEX(COL2, ',', 1) Cno
,SUBSTRING_INDEX(COL3, ',', 1) Sno
FROM MyTargetTable
Result:
Pno Cno Sno
000002 09 007
I might get more than 3 rows in each columns, I would like to know if there is any way to achieve this without specifying the substring position.
Possible input data could also be like this
COL1 COL2 COL3
000002,000003,000042,,000002 09,31,51,,32 007,004,007,,012