I have a table TableA with a tilde operated column ColumnA
TableA
**ColumnA**
123~abc~def~~~~~ghi~j~k~lmn~op~~~
231~a~dfg~wer~~~~~~~hijkl~~~
As we can see in the above two rows, it is '~' separated. I basically want to separate the values into individual columns. There are 15 '~' operators.
My output table should be something like
Col1 Col2 Col3 Col4 . . .. . . .. .. .. .. .
123 abc def . .. . .. .. ... .. . . .
I have a query in DB2 which will do this but it requires 15 subqueries to achieve this task as there are 15 '~' operators. Given below:
SELECT substr(ColumnA, 1, LOCATE('~', ColumnA)-1) AS Col1,
substr(ColumnA, charindex('~', ColumnA)+1, LEN(ColumnA)) AS Other
FROM TableA
I am separating Col1 only by the above query. If I wish to separate 15 columns, I will have to subquery this 15 times.
Is there a better way to do this?
Thank you