SQL SERVER 正则匹配实例


create function dbo.RegexMatch (     

@pattern varchar(2000),     

@matchstring varchar(8000) 

) 

returns int

as

begin    

    declare @objRegexExp int    

    declare @strErrorMessage varchar(255)     

    declare @hr int,@match bit    

    exec @hr= sp_OACreate 'VBScript.RegExp', @objRegexExp out    

    if @hr = 0         

        exec @hr= sp_OASetProperty @objRegexExp, 'Pattern', @pattern     

    if @hr = 0         

        exec @hr= sp_OASetProperty @objRegexExp, 'IgnoreCase', 1     

    if @hr = 0         

        exec @hr= sp_OAMethod @objRegexExp, 'Test', @match OUT, @matchstring     

    if @hr <>0     

    begin        

        return null    

    end    

    exec sp_OADestroy @objRegexExp     

    return @match 

end

 

使用方法:

select name from BS_products  where dbo.RegexMatch('^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+',Intro)=1 

 

select * from dbo.Bs_ProProperty where dbo.RegexMatch('^\d{6}$',code)=1

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM