在 T-SQL 中,如何检查一个字符串是否包含两个或更多相似字符?
我的列包含 nvarchar 的手机号码,可以是“5512111445”、“6612888445”或隐藏号码,如“5512zzz44x”
我有一个用户输入的搜索模式,它可能是“xx12yyy4zx”,我想将所有匹配的数字返回到这个模式,其中 x、y、z 代表任何数字,但如果重复它,它将代表相同的数字。例如,之前的模式应该返回上面列出的所有数字。
xx 是相似的数字,例如 55 或 66.. 而 xy 是不同的数字,例如 45 或 67..
如何做到这一点?
最佳答案
您可以将掩码和数字中的每个字符旋转到列中,然后单独对掩码进行分组,然后是掩码 + 数字。在此方法中,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 字符
EDIT - 对于 GROUP BY n
,包括 ISNUMERIC(n) 和 ISNUMERIC(m) 的三个剩余组合
EDIT - 删除第八个 UNION
关于sql - 在 nvarchar 列中找到两个或更多相似字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183612/