select Comb.Comb + cast(C1.ID as C(1)) from (select CAST(A1.ID as C(1)) + CAST(B1.ID as C(1)) as Comb ; from cur A1 INNER JOIN cur B1 ; ON A1.ID < B1.ID) Comb INNER JOIN cur C1 ON Comb.Test NOT LIKE '%' + cast(C1.ID as (1)) + '%'From the top of my head.