x
1
DECLARE
2
pos integer NOT NULL DEFAULT 0;
3
occur_number integer NOT NULL DEFAULT 0;
4
temp_str varchar;
5
beg integer;
6
i integer;
7
length integer;
8
ss_length integer;
9
BEGIN
10
IF beg_index > 0 THEN
11
beg := beg_index;
12
temp_str := substring(string FROM beg_index);
13
14
FOR i IN 1..occur_index LOOP
15
pos := position(string_to_search IN temp_str);
16
17
IF i = 1 THEN
18
beg := beg + pos - 1;
19
ELSE
20
beg := beg + pos;
21
END IF;
22
23
temp_str := substring(string FROM beg + 1);
24
END LOOP;
25
26
IF pos = 0 THEN
27
RETURN 0;
28
ELSE
29
RETURN beg;
30
END IF;
31
ELSE
32
ss_length := char_length(string_to_search);
33
length := char_length(string);
34
beg := length + beg_index - ss_length + 2;
35
36
WHILE beg > 0 LOOP
37
temp_str := substring(string FROM beg FOR ss_length);
38
pos := position(string_to_search IN temp_str);
39
40
IF pos > 0 THEN
41
occur_number := occur_number + 1;
42
43
IF occur_number = occur_index THEN
44
RETURN beg;
45
END IF;
46
END IF;
47
48
beg := beg - 1;
49
END LOOP;
50
51
RETURN 0;
52
END IF;
53
END;