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
 Display Row data in Column wise based on ID passed

Author  Topic 

shtapi
Starting Member

2 Posts

Posted - 2009-11-18 : 17:08:56
i have data like below...
ITEMID STYLEID STYNAME COLOR WIDTH
8 2044-226 Jzz Oriinal Green-black M
1 1044-222 Jzz Oriinal Pink-orange W
9 2044-230 Jzz Oriinal Gray-blue M
12 2044-214 Jzz Oriinal Gray-white M
6 1044-155 Jzz Oriinal RED-Lavender W

i want this data to be displayed like..
if i pass item ID 8 then
------------------------
Col1 Col2
------------------------
Item Id 8
Style ID 2044-226
Style Name Jzz Oriinal
Color Green-black
Width M

is this possible how ?

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-11-19 : 00:05:12
try like this

DECLARE @t TABLE (ITEMID varchar(32),STYLEID varchar(32),STYNAME varchar(32),COLOR varchar(32),WIDTH varchar(32))
insert into @t select
'8', '2044-226', 'Jzz Oriinal', 'Green-black', 'M' union all select
'1', '1044-222', 'Jzz Oriinal', 'Pink-orange', 'W' union all select
'9', '2044-230', 'Jzz Oriinal', 'Gray-blue', 'M' union all select
'12', '2044-214', 'Jzz Oriinal', 'Gray-white', 'M' union all select
'6', '1044-155', 'Jzz Oriinal', 'RED-Lavender', 'W'

SELECT col1,col2
from (select * from @t where itemid = 8)a
unpivot (col2 for col1 in ([itemid],[styleid],[styname],[color],[width]))p

Go to Top of Page

shtapi
Starting Member

2 Posts

Posted - 2009-11-19 : 09:24:23
Thank You very much for very quick answer!!
This is what i was looking for..
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-11-19 : 23:44:17
quote:
Originally posted by shtapi

Thank You very much for very quick answer!!
This is what i was looking for..



welcome
Go to Top of Page
   

- Advertisement -