Click here to Skip to main content
15,915,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have some data as follows
HTML
coverage services medical
  500      1000    1500

i need sql output as follows
HTML
type    desc     value
 A     coverage   500
 B     services   1000
 C     medical    1500 


Thanks in advance
Posted

Try with this solution I have tried it in AdventureWorks2008 Database

SQL
USE AdventureWorks2008

SELECT ColName,data
FROM HumanResources.vEmployee
UNPIVOT
(
	Data
	For ColName IN ([FirstName],[EmailAddress])
)Unpiv
 
Share this answer
 
Comments
a.pkumar 17-Sep-13 7:16am    
thank u
it is working fine
SQL
Create Table #Temp
		(
		coverage Real,
		services Real,
		medical Real
		)

Insert into #Temp 
Values(500,1000,1500)

Select Char(64+Row_Number() Over(Order by value)) [type],[desc],[value] 
From 
   (Select coverage,services,medical From #Temp)p
UNPIVOT 
   (value for [desc] in (coverage,services,medical)) unpvt

-- Output:
--type	desc	   value
-----	-------    ------
--A	coverage   500
--B	services   1000
--C	medical	   1500

Drop Table #Temp 
 
Share this answer
 
v6
this is an example to show how to convert columns of one data table to another data table

here columns of data table dtm is converted rows of dt

C#
for (int i = 0; i < dtm.Columns.Count; i++)
                    {
                        DataRow newRow = dt.NewRow();
                        newRow[0] = dtm.Columns[i].Caption;
                        for (int j = 0; j < dtm.Rows.Count; j++)
                        {
                            if (dtm.Rows[j][i].ToString() != "")
                                newRow[j + 1] = dtm.Rows[j][i].ToString();
                        }
                        dt.Rows.Add(newRow);
                    }



i suggest u instead of searching sql query for converting take all your data in data table then convert it
 
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