Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert row of table to column

Author  Topic 

vmurali
Yak Posting Veteran

88 Posts

Posted - 2007-01-17 : 06:22:35
tblname rowno colname colvalue
emp 1 Title Mr
emp 1 firstname XYZ
emp 1 lastname M
I want to get as

title first_name lastname
Mr XYZ M

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-17 : 06:27:35
[code]-- prepare sample data
declare @t table (tblname varchar(3), rowno tinyint, colname varchar(9), colvalue varchar(3))

insert @t
select 'emp', 1, 'Title', 'Mr' union all
select 'emp', 1, 'firstname', 'XYZ' union all
select 'emp', 1, 'lastname', 'M'

-- show the result
select max(case when colname = 'title' then colvalue end) as Title,
max(case when colname = 'firstname' then colvalue end) as FirstName,
max(case when colname = 'lastname' then colvalue end) as LastName
from @t
group by rowno[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -