-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfnc_aux_case_insensitive_replace.sql
More file actions
36 lines (29 loc) · 1.07 KB
/
Copy pathfnc_aux_case_insensitive_replace.sql
File metadata and controls
36 lines (29 loc) · 1.07 KB
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
set term ^ ;
create or alter function aux_case_insensitive_replace(
source_text varchar(16384)
, replacement_part varchar(16384)
, new_part varchar(16384) = null
)
returns varchar(16384)
as
declare prev_source_text varchar(16384);
declare pos bigint;
declare new_part_len bigint;
begin
new_part = coalesce(new_part, '');
new_part_len = char_length(new_part);
while (source_text containing replacement_part) do
begin
prev_source_text = source_text;
pos = position(upper(replacement_part) in upper(prev_source_text));
source_text = substring(prev_source_text from 1 for pos - 1)
|| new_part
|| substring(prev_source_text from pos + new_part_len);
-- to prevent infinity loop compare text before and after replace
if (source_text is not distinct from prev_source_text)
then exit;
end
return source_text;
end^
set term ; ^
comment on function aux_case_insensitive_replace is 'Replaces all occurrences of a substring in a string with case insensitive.';