Header Ads

Convert Row Data to Column Data SQL Server

I got new request from my subscriber to convert row data to column data using sql server.Let me explain you how to do this.
SQL_Server_2008

Row data query is like this:
IdEmpNameAgeSexLocation
1Navas Khan25MaleChennai
And they want output(columndata) like this:
ParameterValue
Id1
EmpNameNavas Khan
Age25
SexMale
LocationChennai
I have used UNPIVOT for this query
SELECT Parameter,Value FROM (select top 1
convert(varchar(max),'1') Id, convert(varchar(max),'Navas Khan') Empname,
convert(varchar(max), '25') Age, convert(varchar(max), 'Male')Sex,
convert(varchar(max),'Chennai') Location
from emp_detail) p
UNPIVOT
(value FOR parameter IN
(id,EmpName, age, sex, Location)
)AS unpvt   
 

No comments:

Powered by Blogger.