By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table t(Sentence varchar(100));
insert into t select 'two';
insert into t select 'four';
insert into t select 'twenty';
insert into t select 'the quick brown fox jumped over the lazy dog longerwordstoo';
4 rows affected
select t.sentence, String_Agg( masked, ' ') within group(order by seq) as Masked
from t
cross apply (
select seq, [value] word,
case
when l<=3 then [value]
when l<=5 then Stuff([value],2,l-2,Replicate('*',l-2))
else
Stuff([value],3,l-4,Replicate('*',l-4))
end Masked
from (
select j.[value], 1 + Convert(tinyint,j.[key]) Seq
from OpenJson(Concat('["',replace(t.sentence,' ', '","'),'"]')) j
)w
cross apply (values(Len([value])))x(l)
)w
group by t.sentence;
sentence | Masked |
---|---|
four | f**r |
the quick brown fox jumped over the lazy dog longerwordstoo | the q***k b***n fox ju**ed o**r the l**y dog lo**********oo |
twenty | tw**ty |
two | two |