您可以将掩码和数字中的每个字符旋转到列中,然后单独对掩码进行分组,然后是掩码 + 编号。在此方法中,5512111445 和 6612888445 与掩码 xx12yyy4yz 不匹配,因为掩码中的 y 未映射到唯一数字。但是,手机号码 5512111415 和 6612888485 匹配掩码 xx12yyy4yz,手机号码 5512zzz44x 也是如此。
--declare @mobileNums varchar(10)='5512111445'; --no match because @mask y maps to different values
--declare @mobileNums varchar(10)='6612888445'; --no match because @mask y maps to different values
--declare @mobileNums varchar(10)='5512111415'; --no match because @mask x should not equal @mask z
--declare @mobileNums varchar(10)='6612888485'; --matches
--declare @mobileNums varchar(10)='8812888485'; --no match because @mask x should not equal @mask y
--declare @mobileNums varchar(10)='5512zzz44x'; --matches because z and x are both hidden and different
--declare @mask varchar(10)='xx12yyy4yz';
declare @mobileNums varchar(10)='3211zyy'; -- no match because @mask y <> @mask z, but @mobileNums y = y
declare @mask varchar(10)='3211yxz';
declare @t table(n char, m char);
declare @i int=1;
while @i<=LEN(@mobileNums) begin
insert into @t values (SUBSTRING(@mobileNums,@i,1), SUBSTRING(@mask,@i,1));
set @i+=1;
end
if exists(
-----------------------------------------------------------------------------
-- Group by m
select
m, c=count(m)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
group by m
except
select
m, c=count(m+n)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
group by m,n
union
select
m, c=count(m)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
group by m
except
select
m, c=count(m+n)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
group by m,n
union
select
m, c=count(m)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
group by m
except
select
m, c=count(m+n)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
group by m,n
union
select
m, c=count(m)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=1
group by m
except
select
m, c=count(m+n)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=1
group by m,n
union
-----------------------------------------------------------------------------
-- Group by n
-- Add a rule that no numeric @mobileNums digit can correspond to more than one alpha @mask character
select
n, c=count(m)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
group by n
except
select
n, c=count(m+n)
from @t
where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
group by m,n
union
-- For GROUP BY n, include the three remaining combinations of ISNUMERIC(n) and ISNUMERIC(m)
select
n, c=count(m)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
group by n
except
select
n, c=count(m+n)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
group by m,n
union
select
n, c=count(m)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
group by n
except
select
n, c=count(m+n)
from @t
where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
group by m,n
)
select patMatch='False'
else
select patMatch='True';
编辑- 添加一个规则,没有数字 @mobileNums 数字可以对应多个字母 @mask 字符
编辑- 对于GROUP BY n
,包括 ISNUMERIC(n) 和 ISNUMERIC(m) 的其余三个组合
编辑- 删除第八个UNION