Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the data like below

The data looks like this:

Location Users
-------------------------------------------------------
USA Jhon, Ram
Japan Kevin, Kenady
India Michal, Joy, Selvin, Mohan

I want the out put as

Users
------------------------------
Jhon, Ram, Kevin, Kenady, Michal, Joy, Selvin, Mohan


Please let me know how can i proceed with this.

Thanks in advacne
Posted

Use this code

SQL
declare @tbl1 Table (location varchar(50), Users varchar(100))
insert into @tbl1
select 'USA', 'Jhon, Ram'
union
select 'Japan', 'Kevin, Kenady'
union
select 'India', 'Michal, Joy, Selvin, Mohan'

SELECT  STUFF((SELECT ','+ Users FROM @tbl1 nolock order by users for XML path('') ),1,1,'')  as Users
 
Share this answer
 
HI,

You can do it using following link. Please go through them.

1. Convert row data to column in SQL Server
2. SQL Server convert columns to rows
3. Convert rows to columns in SQL

Once you can able to get the column data in rows then concatenate them and this will produce the result for you. :D

Thanks
 
Share this answer
 
Try something like this

string total = string.Empty;
DataTable table = new DataTable();
table.Columns.Add("Location", typeof(string));
table.Columns.Add("Users", typeof(string));


table.Rows.Add("USA","Jhon, Ram");
table.Rows.Add("Japan", "Kevin, Kenady");
table.Rows.Add("India", "Michal, Joy, Selvin, Mohan");
String str = "";
table.Rows.Cast<datarow>().ToList().ForEach((datarow) => { str += datarow["Users"].ToString() + ","; });


str = str.TrimEnd(',');
 
Share this answer
 
v2

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