Sql 更新值不存在的位置';t匹配management studio 2012中的正则表达式格式

Sql 更新值不存在的位置';t匹配management studio 2012中的正则表达式格式,sql,regex,sql-update,Sql,Regex,Sql Update,我需要验证英国电话号码,基本上删除任何无效的。我很想在前端验证它们,但遗憾的是这不可能 花了很长时间寻找,我在这里找到了一个极好的答案: 证明:/^(?0(*\d){9,10}$/ 然后从这个问题(以及其他问题): 建议使用xp_pcre:启用并使用正则表达式,但这与64位不兼容 所以我的问题是如何在值与正则表达式格式不匹配的字段上执行update语句 这里有一个使用标准SQL功能(即没有CLR功能)的解决方案: 注:我假设当你说“有效的英国电话号码”时,你指的是一个对英国电话有效的电话号码;与

我需要验证英国电话号码,基本上删除任何无效的。我很想在前端验证它们,但遗憾的是这不可能

花了很长时间寻找,我在这里找到了一个极好的答案: 证明:/^(?0(*\d){9,10}$/

然后从这个问题(以及其他问题): 建议使用xp_pcre:启用并使用正则表达式,但这与64位不兼容


所以我的问题是如何在值与正则表达式格式不匹配的字段上执行update语句

这里有一个使用标准SQL功能(即没有CLR功能)的解决方案:


注:我假设当你说“有效的英国电话号码”时,你指的是一个对英国电话有效的电话号码;与从英国打电话的有效号码相反(即,这将显示我们的电话号码无效)。

正则表达式允许
0 1)2)3)4)5)6)7)8)9)0
为有效号码,因此我不会将其归类为“美妙的”@MartinSmith它检查正确的长度,以0开头并允许空格,所有这一切都很好,好像我不能改变前端,我知道只有数字可以通过。我还主要提到了前一个问题,为了不被指责在提问之前没有尝试寻找答案。SQL Server不支持正则表达式,但您可以使用向SQL查询公开.NET正则表达式
create function dbo.PhoneNoIsValid
(@number nvarchar(20))
returns bit
begin
    --use an innocent until proven guilty approach
    --once proven guilty, skip further checks by adding  
    --`if @isValid = 1 and` before further checks 
    declare @isValid bit = 1
    --no strict rules around spaces; they are allowed but  
    --don't add anything
    --by removing them we simplify the patterns we need to check
    set @number = REPLACE(@number,' ','')
    --aside from spaces, only numbers, brackets, and the plus  
    --sign are valid chars
    if @number like '%[^\+\(\)0-9]%' 
        set @isValid = 0
    --min length of a valid phone number is 11 chars
    if @isValid = 1 and LEN(@number) < 11  
        set @isValid = 0
    --the area code (minus leading zero (or similar) plus the  
    --local code are only numbers (and spaces; removed earlier)  
    --so we can check for invalid chars.
    if @isValid = 1 and SUBSTRING(@number,LEN(@number)-9,10) like '%[^0-9]%'  
        set @isValid = 0
    --now we've validated the last bit, remove it so we can  
    --focus on the first bit
    if @isValid = 1  
        set @number = SUBSTRING(@number,1, LEN(@number)-10)
    --given we're using a UK number there are limited options;  
    --so simplest to just enumerate these and check against  
    --each valid option
    if @number not in ('0','0044','+44','+44(0)','0044(0)')  
        set @isValid = 0
    --that's all the checks I can think of; at this stage the  
    --number's valid or has been proven invalid.
    return (@isValid)
end
declare @sampleData table 
(
    phoneNo nvarchar(20)
    , isValid bit default(1)
)
insert @sampleData 
(phoneNo)
values ('0044 1234 567890')
, ('+44 1234 567891')
, ('+44 (0)1234 567892')
, ('0044 (0)1234 567892')
, ('01234 567893')
, ('00441234567890')
, ('+441234567891')
, ('+44(0)1234567892')
, ('0044(0)1234567892')
, ('01234567893')
insert @sampleData 
(isValid, phoneNo)
values (0,'0044 1234 56780')
, (0,'+44 1234 56781')
, (0,'+44 (0)1234 56782')
, (0,'044 (0)1234 567893')
, (0,'1234 567894')
, (0,'234567895')
, (0,'0044123456786')
, (0,'+44123456787')
, (0,'+44(0)123456788')
, (0,'044(0)1234567899')
, (0,'1234567810')
, (0,'234567811')
--select * from @sampleData 

--demo
select *
from @sampleData 
where dbo.PhoneNoIsValid(phoneNo) != isValid --show where I've got something wrong

--update statement
update @sampleData
set phoneNo = ''
where dbo.PhoneNoIsValid(phoneNo)= 0

select isValid, COUNT(1) from @sampleData group by isValid order by isValid
select isValid, COUNT(1) from @sampleData where phoneNo = '' group by isValid order by isValid