If you know how long your information field will be, you can use the static version, which will give another answer, but you can always create a function to create it:
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
For this function, you pass in the values ββyou want to hide. So your query will look like this:
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
Result:
| COL1 | ---------------- | 384XXXXXX434 | | 743XXXXXX878 | | 111XXXXXX885 |
Or is this a way to do this with a 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