SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Convert column values to column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

abhit_kumar
Posting Yak Master

146 Posts

Posted - 01/22/2014 :  06:14:14  Show Profile  Reply with Quote
Dear All,

I have a query, where i have a table where two columns is there.

I want to convert first column records in Column header and second column records would come under that header.


Col1	Col2		
FirstName     	Ajay		
LastName	Nath		
DOB	25/08/1981		
Gender	Male		
			
Output			
			
FirstName     	LastName	DOB	Gender
Ajay	Nath	25/08/1981	Male



Please help me

stepson
Constraint Violating Yak Guru

Romania
413 Posts

Posted - 01/22/2014 :  06:42:03  Show Profile  Reply with Quote


with aCTE
AS (
		select 'FirstName' as Col1, 'Ajay' as Col2 union all
		select 'LastName',	'Nath' union all
		select 'DOB',	'25/08/1981' union all
		select 'Gender'	,'Male'
		)
		


select 
*
from 
	aCTE
	Pivot(
		max(col2) FOR col1 in ([FirstName],[LastName],[DOB],[Gender])) AS x;				



Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000