SQL Server正则表达式 替换函数应用详解
时间:2024-01-28 06:01:16
--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 表
*/
标签:sql正则表达式,替换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python安装教程
2021-06-13 09:22:49
![](https://img.aspxhome.com/file/2023/8/90438_0s.jpg)
python实现FTP文件传输的方法(服务器端和客户端)
2021-03-29 23:36:08
![](https://img.aspxhome.com/file/2023/9/65979_0s.png)
mysql UNIX时间戳与日期的相互转换
2010-02-10 12:10:00
python实现字典(dict)和字符串(string)的相互转换方法
2021-10-19 18:22:44
使用python+whoosh实现全文检索
2021-01-04 05:29:46
Python利用Matplotlib库实现绘制饼形图
2021-08-13 11:08:51
![](https://img.aspxhome.com/file/2023/2/95132_0s.png)
Vue中使用webpack别名的方法实例详解
2024-05-11 09:13:41
对tf.reduce_sum tensorflow维度上的操作详解
2023-01-07 14:10:28
网页开发中的容易忽略的问题 javascript HTML中的table
2023-09-04 20:43:52
SQL SERVER 2005中的同步复制技术
2009-01-05 13:44:00
JavaScript中函数声明与函数表达式的区别详解
2024-04-19 10:02:34
django写单元测试的方法
2021-02-04 19:17:28
![](https://img.aspxhome.com/file/2023/4/110414_0s.png)
python实现删除文件与目录的方法
2023-11-12 23:34:46
Python OpenCV对图像进行模糊处理详解流程
2022-05-16 03:54:19
![](https://img.aspxhome.com/file/2023/5/86675_0s.png)
Python 实现自动完成A4标签排版打印功能
2021-07-14 22:55:15
Asp.net 连接MySQL的实现代码[]
2024-01-16 04:27:15
网马解密大讲堂——网马解密中级篇(Document.write篇)
2009-09-16 16:16:00
python中通过selenium简单操作及元素定位知识点总结
2021-07-18 15:34:37
![](https://img.aspxhome.com/file/2023/5/95285_0s.png)
人工智能(AI)首选Python的原因解析
2021-03-23 16:04:12
![](https://img.aspxhome.com/file/2023/3/112093_0s.jpg)
django序列化serializers过程解析
2022-09-26 22:22:36