数据库教程:SQL Server正则表达式 替换函数应用详解

–sql正则替换函数 复制代码 代码如下: create function dbo.regexreplace ( @source ntext, –原字符串 @regex

–sql正则替换函数

复制代码 代码如下:
create function dbo.regexreplace
(
@source ntext, –原字符串
@regexp varchar(1000), –正则表达式
@replace varchar(1000), –替换值
@globalreplace bit = 1, –是否是全局替换
@ignorecase bit = 0 –是否忽略大小写
)
returns varchar(1000) as
begin
declare @hr integer
declare @objregexp integer
declare @result varchar(5000)
exec @hr = sp_oacreate ‘vbscript.regexp’, @objregexp output
if @hr <> 0 begin
exec @hr = sp_oadestroy @objregexp
return null
end
exec @hr = sp_oasetproperty @objregexp, ‘pattern’, @regexp
if @hr <> 0 begin
exec @hr = sp_oadestroy @objregexp
return null
end
exec @hr = sp_oasetproperty @objregexp, ‘global’, @globalreplace
if @hr <> 0 begin
exec @hr = sp_oadestroy @objregexp
return null
end
exec @hr = sp_oasetproperty @objregexp, ‘ignorecase’, @ignorecase
if @hr <> 0 begin
exec @hr = sp_oadestroy @objregexp
return null
end
exec @hr = sp_oamethod @objregexp, ‘replace’, @result output, @source, @replace
if @hr <> 0 begin
exec @hr = sp_oadestroy @objregexp
return null
end
exec @hr = sp_oadestroy @objregexp
if @hr <> 0 begin
return null
end
return @result
end

/*
配置对扩展存储过程的支持
microsoft sql server 2005 -> 配置工具 -> 外围应用配置器 -> 功能的外围应用配置 -> ole自动化:支持ole自动化
使用举例1:

复制代码 代码如下:
declare @source nvarchar(4000)
set @source = ‘dsafsdf’
select dbo.regexreplace(@source, ‘<[^>]+>’, ”, 1, 1)

使用举例2: (将数据库字段中含有<font color=’#ff0000′>aaa</font>替换为<font>aaa</font>)
select id,dbo.regexreplace(字段,'<font([^>])*>’,'<font>’,1,0) as 别名 from 表
*/

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/617102.html

(0)
上一篇 2021年5月20日
下一篇 2021年5月20日

精彩推荐