Tuesday 18 April 2017

Permutation and Combination in SQL Server :

declare @String varchar(10) = 'TIGER'
;with s(t,n)
as 
(
select substring(@String,1,1),1
union all
select substring(@String,n+1,1),n+1
from s where n<len(@String)
)
,j(t) 
as 
(
select cast(t as varchar(10)) from s
union all
select cast(j.t+s.t as varchar(10))
from j,s where patindex('%'+s.t+'%',j.t)=0
)
select t from j where len(t)=len(@String)

No comments:

Post a Comment