マスクと数字の各文字を列にピボットしてから、マスクのみでグループ化し、その後にマスク+数字を続けることができます。この方法では、マスクのyが一意の数字にマップされないため、5512111445と6612888445はマスクxx12yyy4yzと一致しません。ただし、携帯電話番号5512111415および6612888485は、携帯電話番号5512zzz44xと同様に、マスクxx12yyy4yzと一致します。
--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桁が複数のアルファ@マスク文字に対応できないというルールを追加します
編集-の場合GROUP BY n
、ISNUMERIC(n)とISNUMERIC(m)の残りの3つの組み合わせを含めます
編集-8番目を削除しますUNION