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

I have a table(may be DataTable) as

ID Name Department
1  ABC  Oracle
2  DEF  JAVA
3  GHI  C#


Now I want this output should be :

Column1    Column2
ID         1,2,3
Name       ABC,DEF,GHI
Department Oracle,JAVA,C#


Please help me in C# or SQL code.
Thanks in advance.
Posted
Updated 3-Jan-13 5:34am
v2

Hi,
i'm not sure if this is the best approach but you can:

- fill a datatableobject in c# with a
SQL
SELECT * FROM myTable

and the DataAdapter. http://msdn.microsoft.com/de-de/library/bh8kx08z%28v=vs.80%29.aspx[^]

- then i would make a list for each column and fill them with the entrys of each row.

- Otherwise, if you want to have a DataTable as result, your can use a doubled foreach loop to handle stuff like this. Or you have a look at:

Transpose a DataTable using C#[^]


hope this helps.
 
Share this answer
 
 
Share this answer
 
Hi
You can use from bellow code:

Declare		@ID		nVarChar(500),
		@Name		nVarChar(500),
		@Department	nVarChar(500)
-------------------------------------------------------------------------------
Select	@ID		= N'',
	@Name		= N'',
	@DepartMent	= N''	
------------------------------------------------------------------------------
Select	@ID		= @ID		+ ', ' + Cast(ID As VarChar),
	@Name		= @Name		+ ', ' + Name,
	@Department	= @Department	+ ', ' + Department 
	From	[YourTableName]
-------------------------------------------------------------------------------
Select	@ID		= Substring(@ID, 2, Len(@ID)),
	@Name		= Substring(@Name, 2, Len(@Name)),
	@Department	= Substring(@Department, 2, Len(@Department))
-------------------------------------------------------------------------------
Select	N'ID', @ID
Union
Select	N'Name', @Name
Union
Select	N'Department',@Department



If you want using this action dynamically, please tell me to write dynamic version for you.

I hope it's helpful
 
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