Hi all i am newbie in SQL i have a table in which there is a column named dilution_name in this column there are values coming in comma separated format like A,B,C etc. also these values may vary like in some row the values are A,B,C and in some case its like A,B,C,D i just want to separate these values and print them in multiple column if there is only 3 comma separated values then there should be 3 values in comma would be written rest should be null I have tried
select ParsedData.*
from dilution_table mt
cross apply ( select str = mt.dilution_name + ',,' ) f1
cross apply ( select p1 = charindex( ',', str ) ) ap1
cross apply ( select p2 = charindex( ',', str, p1 + 1 ) ) ap2
cross apply ( select p3 = charindex( ',', str, p2 + 2 ) ) ap3
cross apply ( select p4 = charindex( ',', str, p3 + 3 ) ) ap4
cross apply ( select p5 = charindex( ',', str, p4 + 4 ) ) ap5
cross apply ( select p6 = charindex( ',', str, p5 + 5 ) ) ap6
cross apply ( select val1 = substring( str, 1, p1-1 )
, val2 = substring( str, p1+1, p2-p1-1 ),
val3 = substring( str, p2+1, p2-p1-1 ),
val4 = substring( str, p3+1, p2-p1-1 ),
val5 = substring( str, p4+1, p2-p1-1 ),
val6 = substring( str, p5+1, p2-p1-1 ),
val7 = substring( str, p6+1, p2-p1-1 )
) ParsedData
[sample data][1]