October 18, 2010 at 7:49 am
I have a table like below:
Emp
====
id name
-- -----
1 x
now, i want the resuts in the below format:
col1 col2
-- ---
id 1
name x
Can someone tell me how should achieve it using UNPIVOT
thanks
erajendar
October 18, 2010 at 3:04 pm
Please decide by yourself which option is easier to read....
DECLARE @tbl TABLE
(
id INT, name VARCHAR(30)
)
INSERT INTO @tbl SELECT 1 ,'x'
-- option a
SELECT 'id' AS col1,CAST(id AS VARCHAR(30)) AS col2 FROM @tbl
UNION ALL
SELECT 'name',name FROM @tbl
-- option b
SELECT col1,col2
FROM
(SELECT CAST(id AS VARCHAR(30)) AS id,name
FROM @tbl
) p
UNPIVOT
(col2 FOR col1 IN
(id,name)
)AS unpvt
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply