try this:--datadeclare @temp table(data varchar(10))insert into @tempselect 'aaaa' union allselect 'aaaa''' union allselect 'erty' union allselect 'asdf' union allselect 'asd8das' union allselect 'hello''' union allselect 'hello' --solutionselect x.data2 as combination1,t.data as combination2from (select replace(data,'''','') as data2from @tempgroup by replace(data,'''','') having count(replace(data,'''','')) = 2) as x cross join @temp as t where t.data like x.data2 + '%'and len(t.data) > len(x.data2)