Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi coder,

I have a table in which I have to columns

DPS Skill
X A
X B
Y c
Y D

how to get the data in

DPS Skill
X A,B
Y C,D

And store them in DataTable in c# I search few code but that doesn't match my requirement any help will be appreciated.

Thanks in Advance.

What I have tried:

Till now I tried to

C#
StringBuilder skills = new StringBuilder();
                    for (int i = 0; i < dtSkills.Rows.Count; i++)
                    {
                        for (int j = 0; j < dtSkills.Rows.Count; j++)
                        {
                            if (dtSkills.Rows[i]["DemandPrimarySkill"].ToString().Equals(dtSkills.Rows[j]["DemandPrimarySkill"].ToString()))
                            {
                                if (dtSkills.Rows[i]["Skill"].ToString().ToUpper().Trim().Equals(dtSkills.Rows[j]["Skill"].ToString().ToUpper().Trim()))
                                {
                                    if (!String.IsNullOrEmpty(skills.ToString()))
                                    {
                                        skills.Append(",");
                                    }
                                    skills.Append(dtSkills.Rows[j]["Skill"].ToString());
                                }
                            }
                            else
                            {
                                break;
                            }

                        }

           }


in this I have dtSkill in which I have all the DPS and related to that DPS I have skills I try to append skill with string builder but not got what I desire.
Posted
Updated 26-Jul-16 4:42am
v2

You can achieve such result using STUFF()
Check following example-
SQL
DECLARE @tbl AS TABLE(DPS VARCHAR(10),Skill VARCHAR(10))
INSERT INTO @tbl
SELECT 'X','A'
UNION ALL
SELECT 'X','B'
UNION ALL
SELECT 'Y','C'
UNION ALL
SELECT 'Y','D'


SELECT DPS,
STUFF((SELECT ', ' + A.Skill FROM @tbl A
WHERE A.DPS=B.DPS FOR XML PATH('')),1,1,'') AS [Skill]
From @tbl B
GROUP BY DPS


Hope, it helps :)
 
Share this answer
 
Comments
amitesh1989 26-Jul-16 5:23am    
@suvendu -This table is just an example their are 1000 data in the table in DB. Database is already create I just need to fetch the data from it and store it in Datatable or how can I use declare command in code behind.
Richard Deeming 26-Jul-16 10:24am    
If any of the values in the Skill column contain "special" characters, they will be XML-encoded in the output.

You can work around that by using FOR XML PATH(''), TYPE, and then adding .value('.', 'varchar(max)') before the comma. Anith Sen posted a detailed overview of the different options back in 2008:
Concatenating Row Values in Transact-SQL[^]
Try this, should help you to solve your request.

DemoTable:
SQL
CREATE TABLE Demo 
(
  DPS CHAR(30),
  SKILL CHAR(30)
);


Test Data:
SQL
INSERT INTO Demo (DPS, SKILL) VALUES
('X', 'A'),
('X', 'B'),
('Y', 'C'),
('Y', 'D');


Test SQL:
SQL
SELECT  DPS,
        SKILLVALS= STUFF((SELECT RTRIM(SKILL) + ','
                          FROM Demo D2
                          WHERE D2.DPS = D1.DPS
                          ORDER BY D2.SKILL
                          FOR XML PATH('')), 1, 0, '')
FROM Demo D1
GROUP BY D1.DPS
ORDER BY D1.DPSS


Test Result:
DPS  SKILLVALS
---  ---------
X    A,B,
Y    C,D,


Small drawback: The ',' at the end of SKILLVALS.

And no, I did not find this solution by studying the MSDN documentation :)
Here you find the information: SQL Server Grouped Concatenation - SQLPerformance.com[^], search for 'FOR XML PATH' there.

I hope it helps.

[Edit] Thanks to Richard Deeming - Professional Profile[^], see his comment to the above solution.
SQL
SELECT DPS,
       SKILLVALS= STUFF((SELECT ',' + RTRIM(SKILL) 
                         FROM Demo D2
                         WHERE D2.DPS = D1.DPS
                         ORDER BY D2.SKILL
                         FOR XML PATH('')), 1, 1, '')
FROM Demo D1
GROUP BY D1.DPS
ORDER BY D1.DPS


Finally the result without leading or trailing ',':
DPS  SKILLVALS
---  ---------
X    A,B
Y    C,D
 
Share this answer
 
v4
Comments
Richard Deeming 26-Jul-16 10:21am    
If you move your comma before the column name, and change the STUFF arguments from 1, 0, '' to 1, 1, '', then you won't have a leading or trailing comma.

However, this is effectively identical to solution 1.
[no name] 26-Jul-16 10:25am    
Wow, great! Thank you very much.

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