Home > Database > Mysql Tutorial > SQL Server替换函数应用详解

SQL Server替换函数应用详解

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:16:43
Original
2047 people have browsed it

--SQL正则替换函数 代码如下: CREATE function dbo.regexReplace ( @source ntext, --原字符串 @regexp varchar(1000), --正则表达式 @replace varchar(1000), --替换值 @globalReplace bit = 1, --是否是全局替换 @ignoreCase bit = 0 --是否忽略大小写 ) r

--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: (将数据库字段中含有aaa替换为aaa)
Select id,dbo.regexReplace(字段,'])*>','',1,0) AS 别名 From 表
*/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
server
From 1970-01-01 08:00:00
0
0
0
Configure server
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template