I want to split each name for individual columns
create table split_test(value integer,Allnames varchar(40))
insert into split_test values(1,'Vinoth,Kumar,Raja,Manoj,Jamal,Bala');
select * from split_test;
Value Allnames
-------------------
1 Vinoth,Kumar,Raja,Manoj,Jamal,Bala
Expected output
values N1 N2 N3 N4 N5 N6 N7.......N20
1 Vinoth Kumar Raja Manoj Jamal Bala
Here is an sql statement using recursive CTE to split names into rows, then pivot rows into columns. SqlFiddle
UPDATE
@KM.'s answer might be a better way to split data into rows without recursive CTE table. It should be more efficient than this one. So I follow that example and simplified the part of null value process logic. Here is the result:
Step 1: Create a table includes all numbers from 1 to a number grater than max length of
Allnames
column.Step 2: Join data of split_test table with numbers table, we can get all the parts start from
,
. Then take the first part between 2,
form every row. If there are null values exists, add them with union.Step 3: Pivot names from rows to columns like my first attempt above, omitted here. SQLFiddle
using this example you can get an idea.