我正在尝试在可变长度字符串中找到所有可能的字符组合。
例如:“--”将有 2^n = 2^2 = 4 种可能性,“x-”、“-x”、“xx”、“--”
我认为基本上我需要循环 c(2,2) + c(2,1) + c(2,0) 其中 c(n,r) = n!/ (r! * (nr)!) 但是我在使用 cte 处理事情时遇到了麻烦。到目前为止,随着您向字符串添加字符,一切都开始崩溃了。
使用数字表 -
declare @s varchar(15)
set @s = '--'
;with subset as (
select cast(stuff(@s,number,1,'x') as varchar(15)) as token,
cast('.' + cast(number as char(1)) + '.' as varchar(11)) as permutation,
cast(1 as int) as iteration ,
number
from numbers where number between 1 and len(@s)
union
select @s, '.0.', 1, 0
) ,
combination as ( …
Run Code Online (Sandbox Code Playgroud)