You're on the right track. This code assumes you have a function in place (something like jimf's post) that returns (rowid, val)declare @table1 table (id int, depotID int, custID int, moveSequence varchar(200))insert @table1select 1, 586, 203, '224-1481-224'union all select 2, 586, 203, '224-1685-224'union all select 3, 586, 198, '224-1481-386-224'union all select 4, 586, 789, '224-369-898-224'declare @table2 table (id int, description varchar(20))insert @table2select 224, 'myAddress224'union all select 369, 'myAddress369'union all select 386, 'myAddress386'union all select 898, 'myAddress898'union all select 1481, 'myAddress1481'union all select 1685, 'myAddress1685'select id ,depotid ,custid ,moveSequence ,max([1]) as loc1 ,max([2]) as loc2 ,max([3]) as loc3 ,max([4]) as loc4from ( select t1.id ,t1.depotid ,t1.custid ,t1.moveSequence ,ca.rowid ,ca.val as moves ,t2.description from @table1 t1 cross apply dbo.fn_ParseStr(moveSequence, '-') ca left outer join @table2 t2 on t2.id = ca.val ) dpivot ( max(description) for rowid in ([1],[2],[3],[4])) pgroup by id ,depotid ,custid ,moveSequenceoutput:id depotid custid moveSequence loc1 loc2 loc3 loc4----------- ----------- ----------- ----------------------------------------- -------------------- -------------------- --------------------1 586 203 224-1481-224 myAddress224 myAddress1481 myAddress224 NULL2 586 203 224-1685-224 myAddress224 myAddress1685 myAddress224 NULL3 586 198 224-1481-386-224 myAddress224 myAddress1481 myAddress386 myAddress2244 586 789 224-369-898-224 myAddress224 myAddress369 myAddress898 myAddress224
Be One with the OptimizerTG