The code should work with your modification. Your database should be SQL Server 2005 and above with competibility level above 90, just in case you need this information.
Here is the sample with the new table and column name.
-- create table Hanes_ProcessedContainers(PC_ID int, Slot1 varchar(10), Slot2 varchar(10),Slot3 varchar(10),Slot4 varchar(10), Slot5 varchar(10))
--insert into Hanes_ProcessedContainers values
--(1,'0100102','0200102','0300111','04000102','05000202')
-- ,(2,'0300001','0100202','0211120','05000221','03000102')
-- ,(3,'0311102','0200112','0400002','07000202','03000202')
;WITH mycte AS
(SELECT PC_ID,col, val from (SELECT * FROM Hanes_ProcessedContainers) src
unpivot (val for col in ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5]))unpvt
WHERE right(val,2)='02')
SELECT PC_ID, [Slot1],[Slot2],[Slot3],[Slot4],[Slot5] FROM (select PC_ID,col, val FROM mycte) src
PIVOT(max(val) FOR col IN ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5])) pvt
--drop table Hanes_ProcessedContainers
limno
All-Star
117326 Points
8003 Posts
Moderator
MVP
Re: SQL Column Selection Based on Meeting Criteria
May 23, 2012 01:25 PM|LINK
The code should work with your modification. Your database should be SQL Server 2005 and above with competibility level above 90, just in case you need this information.
Here is the sample with the new table and column name.
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm