Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Column value as Row
Col1 Col2
a 4
b 2
c 1
d 7

I want result in ,-
a b c d
4 2 1 7
Posted

There are many ways for converting Row to column or vice-versa.But the simplest and effective method is to use Pivot in SQL-server.


Thanks
 
Share this answer
 
Use SQL Server PIVOT

[Dynamic Pivoting in SQL Server]
 
Share this answer
 
try this batch

SQL
DECLARE @Ids VARCHAR(200)
DECLARE @Query VARCHAR(MAX)

SELECT @Ids=REPLACE((select DISTINCT Col1 AS 'data()'  from YourTable   for xml path('')), ' ', ',') 

SET @Query = 'SELECT '+ @Ids + ' FROM ( SELECT Col1,Col2 FROM YourTable) AS A'+
              ' PIVOT (SUM(Col2) FOR Col1 IN ('+ @Ids +')) As PVT'

EXEC(@Query)


Thank you
 
Share this answer
 
Use a nested ForEach loop and column = row row index,

and in your for each loop create a row for every column you have
 
Share this answer
 

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