-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.sql
55 lines (40 loc) · 1.19 KB
/
replace.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- released under the MIT License
drop function if exists dbo.regexp_replace;
go
create function dbo.regexp_replace (
@searchstring varchar(4000),
@pattern varchar(4000),
@replacestring 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 begin
return null
end;
set @result='';
exec @res = sp_OACreate 'VBScript.RegExp', @objRegexExp out;
if @res <> 0 begin
return 'VBScript did not initialize!';
end;
exec @res=sp_OASetProperty @objRegexExp, 'Pattern', @pattern;
if @res <> 0 begin
return 'Pattern property set failed!';
end;
exec @res=sp_OASetProperty @objRegexExp, 'IgnoreCase', 0;
if @res <> 0 begin
return 'IgnoreCase option failed!';
end;
exec @res=sp_OAMethod @objRegexExp, 'Replace', @result out, @searchstring, @replacestring;
if @res <> 0 begin
return 'Bad search string!';
end;
exec @res=sp_OADestroy @objRegexExp;
return @result
end;
go