Chỉ trong trường hợp nếu ai đó cần chức năng bảng có giá trị.
Cách tiếp cận 1
create function ftMaskPhone
(
@phone varchar(30),
@mask varchar(50)
)
returns table as
return
with ci(n, c, nn) as (
select
1,
case
when substring(@mask, 1, 1) = '#' then substring(@phone, 1, 1)
else substring(@mask, 1, 1)
end,
case when substring(@mask, 1, 1) = '#' then 1 else 0 end
union all
select
n + 1,
case
when substring(@mask, n + 1, 1) = '#' then substring(@phone, nn + 1, 1)
else substring(@mask, n + 1, 1)
end,
case when substring(@mask, n + 1, 1) = '#' then nn + 1 else nn end
from ci where n < len(@mask))
select (select c + '' from ci for xml path(''), type).value('text()[1]', 'varchar(50)') PhoneMasked
GO
Sau đó áp dụng nó như
declare @mask varchar(50)
set @mask = '(##) #### ####'
select pm.PhoneMasked
from Phones p
outer apply ftMaskPhone(p.PhoneNum, @mask) pm
Cách tiếp cận 2
Hãy rời abowe một cho lịch sử. Dưới đây là một trong nhiều hơn performant.
create function ftMaskPhone
(
@phone varchar(30),
@mask varchar(50)
)
returns table as
return
with v1(N) as (
select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all
select 1 union all select 1 union all select 1 union all select 1 union all select 1
),
v2(N) as (select 1 from v1 a, v1 b),
v3(N) as (select top (isnull(len(@mask), 0)) row_number() over (order by @@spid) from v2),
v4(N, C) as (
select N, isnull(substring(@phone, case when c.m = 1 then row_number() over (partition by c.m order by N) end, 1), substring(@mask, v3.N, 1))
from v3
cross apply (select case when substring(@mask, v3.N, 1) = '#' then 1 end m) c
)
select Value = (
select c + ''
from v4
order by N
for xml path(''), type
).value('text()[1]', 'varchar(50)')
go
Câu trả lời của tôi có giúp bạn không? –