Click here to Skip to main content
Sign Up to vote bad
good
See more: C#SQL-ServerMySQL
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 3 Jan '13 - 4:38
Edited 3 Jan '13 - 5:34


3 solutions

Hi,
i'm not sure if this is the best approach but you can:
 
- fill a datatableobject in c# with a
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.
  Permalink  
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 496
1 Mahesh Bailwal 342
2 Maciej Los 228
3 Aarti Meswania 215
4 Arun Vasu 174
0 Sergey Alexandrovich Kryukov 9,607
1 OriginalGriff 7,214
2 CPallini 3,943
3 Rohan Leuva 3,261
4 Maciej Los 2,758


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid