SQL 正则表达式 模糊替换字段内容

SQL 正则表达式 模糊替换字段内容


2024年1月20日发(作者:)

oracle里有现成的正则替换函数REGEXP_REPLACE,可SQL SERVER里没有 只能自己创建一个

--SQL正则替换函数

CREATE function eplace

(

@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 '', @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

--把字段NewsContent中包含 width='***' height='***'/> 的内容替换成 />

查询

Select NewsID,eplace(NewsContent,'width=([sConten_temp From

Encyclopedia_temp

替换

update Encyclopedia_temp set NewsContent = eplace(NewsContent,'width=([

gt;])*>','/>',1,0) where NewsContent LIKE '% width=%'

更多信息请查看IT技术专栏

gt;])*>','/>',1,0) AS NewsConten_temp From Encyclopedia_temp

替换

update Encyclopedia_temp set NewsContent = eplace(NewsContent,'width=([

gt;])*>','/>',1,0) where NewsContent LIKE '% width=%'

更多信息请查看IT技术专栏


发布者:admin,转转请注明出处:http://www.yc00.com/web/1705712154a1418789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信