长风秋雁
-- 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 matchCREATE FUNCTION [dbo].[RegexReplace]( -- these match exactly the parameters of RegExp @searchstring varchar(4000), @replacestring varchar(4000), @pattern varchar(4000))RETURNS varchar(4000)ASBEGIN 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 @resultEND您需要在SQL中启用Ole Automation Procedures:exec sp_configure 'show advanced options',1; goreconfigure; gosp_configure 'Ole Automation Procedures', 1; goreconfigure; gosp_configure 'show advanced options',0; goreconfigure;go