Można to zrobić z tabeli liczb (master..spt_values) i stuff
w pętli.
declare @Word varchar(10) = 'sql'
declare @T table
(
Word varchar(10)
)
insert into @T values (@Word)
while not exists(select *
from @T
where Word = replicate('-', len(@Word)))
begin
insert into @T(Word)
select distinct stuff(T.Word, N.number, 1, '-')
from @T as T
cross join
master..spt_values as N
where N.type = 'P' and
N.number between 1 and len(@Word) and
stuff(T.Word, N.number, 1, '-') not in (select Word from @T)
end
select *
from @T
http://data.stackexchange.com/stackoverflow/q/122334/
Albo można użyć reqursive CTE
declare @Word varchar(10) = 'sql'
;with C as
(
select @Word as Word,
0 as Iteration
union all
select cast(stuff(Word, N.number, 1, '-') as varchar(10)),
Iteration + 1
from C
cross join
master..spt_values as N
where N.type = 'P' and
N.number between 1 and len(@Word) and
Iteration < len(@Word)
)
select distinct Word
from C
http://data.stackexchange.com/stackoverflow/q/122337/
Aktualizacja
wersja rekursywne CTE jest bardzo powolny jak poi wypowiedziana przez OP w komentarzu. Używając słowa z 7 literami, zwrócono 960800 wierszy z CTE.
+1 i masz wyjście w kolejności, że PO chce –
@ConradFrix - Kolejność nie było zamierzone :). –
Druga odpowiedź jest dobra, ale czas na "przykładowy" ciąg wejściowy wynosi 28 sekund, pierwsza odpowiedź jest bardzo dobra, a czas na "przykładowy" ciąg wejściowy wynosi 0 sekund. – jozi