If you know how long your field of data will be, then you can use the static version that the other answer will produce, but you can always create a function to generate this:
CREATE FUNCTION MixUpCharacters
(
@OrigVal varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
DECLARE @NewVal varchar(max)
DECLARE @OrigLen int
DECLARE @LoopCt int
DECLARE @Part varchar(max) = ''
Declare @PartLength int
SET @NewVal = ''
SET @OrigLen = DATALENGTH(@OrigVal)
SET @LoopCt = 1
SET @Part = SUBSTRING(@OrigVal, 4, len(@OrigVal)-6)
set @PartLength = LEN(@Part)
WHILE @LoopCt <= @PartLength
BEGIN
-- Current length of possible characters
SET @NewVal = @NewVal + 'X'
-- Advance the loop
SET @LoopCt = @LoopCt + 1
END
Return REPLACE(@OrigVal, @Part, @NewVal)
END
For this function you will pass in the values that you want to mask. So your query would be:
declare @temp table
(
col1 varchar(50)
)
insert into @temp
values ('384844033434'), ('743423547878'), ('111224678885')
select dbo.MixUpCharacters(col1) col1
from @temp
See SQL Fiddle with Demo
The result would be:
| COL1 |
----------------
| 384XXXXXX434 |
| 743XXXXXX878 |
| 111XXXXXX885 |
Or here is a way to do it with recursive CTE:
;with data(col1) as
(
select '384844033434'
union all
select '7434235878'
union all
select '111224678885'
),
s1 (col1, repfull) as
(
select col1,
SUBSTRING(col1, 4, len(col1)-6) repfull
from data
),
s2 (col1, item, repfull, r) as
(
select col1,
cast('x' as varchar(max)),
right(repfull, LEN(repfull)-1),
repfull
from s1
union all
select col1,
'x'+ cast(item as varchar(max)),
right(repfull, LEN(repfull)-1),
r
from s2
where len(repfull) >0
)
select REPLACE(col1, r, item) newValue
from
(
select col1, item, R,
ROW_NUMBER() over(partition by col1 order by len(item) desc) rn
from s2
) src
where rn = 1
See SQL Fiddle with Demo