3 回答
TA贡献1757条经验 获得超7个赞
-- MS SQL using VBScript Regex
-- select dbo.RegexReplace('aa bb cc','($1) ($2) ($3)','([^\s]*)\s*([^\s]*)\s*([^\s]*)')
-- $$ dollar sign, $1 - $9 back references, $& whole match
CREATE FUNCTION [dbo].[RegexReplace]
( -- these match exactly the parameters of RegExp
@searchstring varchar(4000),
@replacestring varchar(4000),
@pattern varchar(4000)
)
RETURNS varchar(4000)
AS
BEGIN
declare @objRegexExp int,
@objErrorObj int,
@strErrorMessage varchar(255),
@res int,
@result varchar(4000)
if( @searchstring is null or len(ltrim(rtrim(@searchstring))) = 0) return null
set @result=''
exec @res=sp_OACreate 'VBScript.RegExp', @objRegexExp out
if( @res <> 0) return '..VBScript did not initialize'
exec @res=sp_OASetProperty @objRegexExp, 'Pattern', @pattern
if( @res <> 0) return '..Pattern property set failed'
exec @res=sp_OASetProperty @objRegexExp, 'IgnoreCase', 0
if( @res <> 0) return '..IgnoreCase option failed'
exec @res=sp_OAMethod @objRegexExp, 'Replace', @result OUT,
@searchstring, @replacestring
if( @res <> 0) return '..Bad search string'
exec @res=sp_OADestroy @objRegexExp
return @result
END
您需要在SQL中启用Ole Automation Procedures:
exec sp_configure 'show advanced options',1;
go
reconfigure;
go
sp_configure 'Ole Automation Procedures', 1;
go
reconfigure;
go
sp_configure 'show advanced options',0;
go
reconfigure;
go
- 3 回答
- 0 关注
- 847 浏览
添加回答
举报