Click here to Skip to main content
14,982,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ID	ErroCode	 Scode	     
1	E1 , E2	     S1	          
2	E1,E2,E3	 S2,S3	      
3	E4	         S4 ,S2, S6	  
4	E1,E2	     S3	          
5	E1,E2,E3,E4	 S1,S2


I have a datatble from datatbse having csv values in one column.I nned to split csv values to multiple columns.Problem is that my csv values are not fixed ,they can be multiple
Please help me to implement above output in second table using c#.

Expected Output in Gridview
ErroCode     Errorcode1 Errorcode2  Errorcode3  Errorcode4  Scode   Scode1  Scode2

E2,E2        E1          E2                                 S1      S1          
E1,E2,E3     E1          E2         E3                      S2,S3   S2      S3      
E4           E4                                             S4,S2   S4      S2  
E1,E2        E1          E2                                 S3      S3          
E1,E2,E3,E4  E1          E2         E3           E4         S1,S2   S1      S2


What I have tried:

I tried but didnt get same output
Posted
Updated 9-May-21 7:15am

BAsically, your problem is not in splitting CSV data (which is trivial) but in storing data as CSV inside a DB in the first place.

It's not a good idea: it makes life considerably more complicated than it needs to be, with the sole exception of INSERT new rows.
Instead, create a new table which links the ID to the Error Code via a foreign key and use a JOIN to "put them back together". It's much, much easier to work with and gets rid of all the problems like "How do I delete an error code" and "how do I insert an error code" which are a real problem in SQL because it's string handling is so poor.

Don't waste time trying to bodge round a broken design, fix your DB instead.
   
Comments
shijikkk 10-May-21 0:56am
   
I have used comma as separator just for reference.it could be any delimiter,In my case I am storing full path which is having">" as separator
OriginalGriff 10-May-21 2:18am
   
It doesn't matter exactly what the separator character is: the problem is the same.
It's a poor DB design decision, made to make INSERT operations easier - which causes real problems with all future uses of the data. SQL is very poor at string handling, and any storage of "multiple values" in a single column causes problems.
   
Comments
shijikkk 10-May-21 0:56am
   
I need to find solution in the c# code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900