Friday, 6 September 2013

Convert to comma separated variables in row-column combination by mapping them one to one


Convert to comma separated variables in row-column combination by mapping them one to one



If you have two varibles with comma separated values.First one is 1,2,3 and second is 4,5,6 and you want to map one to one and want it in rows and column form like :-
1 4
2 5
3 6


To achieve result like above,you can use logic in query like below : -



  declare @temp1 varchar(100);
  declare @temp2 varchar(100);

  set @Temp1='1,2,3';
  set @Temp2='4,5,6';


  select OrderID,Order1 from (
  select *,DENSE_RANK() over (order by Order1) as Num1,
  DENSE_RANK() over (order by OrderID) as Num2 from (
  (select s.OrderID,s1.OrderId as Order1
  from dbo.splitstring(@Temp1) s,dbo.splitstring(@Temp2) s1)) temp
  ) temp2
  where Num1=Num2

No comments:

Post a Comment