I've written an Oracle function, which replace some symbols to other. Example of string:
SELECT ReplaceStringFormat('Employee with FIO <FIO>') FROM dual;
Result after replace:
'Employee with FIO '||FIO||''
And I have an error ORA-12725 when string begins with bracket or end with bracket like this:
SELECT ReplaceStringFormat('(<DEPTID>) have <EMP_NUMB> employees') FROM dual;
or
SELECT ReplaceStringFormat('Employee <FIO> works in dept (<DEPTID>)') FROM dual;
I'm a newbie in REGEXP.. Please, explain me, how can I solve my problem.
Function's code:
CREATE OR REPLACE FUNCTION ReplaceStringFormat (p_source_string IN VARCHAR2)
RETURN VARCHAR2 IS
v_result_string VARCHAR2(4000);
v_counter BINARY_INTEGER := 1;
v_flag NUMBER(1);
v_last_char CHAR(1);
v_last_char_new VARCHAR(5);
v_first_char CHAR(1);
v_first_char_new VARCHAR(5);
v_first_char_flag NUMBER(1) := 0;
BEGIN
v_result_string := p_source_string;
v_flag := 0;
WHILE v_counter <= 2 LOOP
IF v_flag = 0 THEN
IF INSTR (v_result_string, '<') = 1 THEN
v_result_string := REGEXP_REPLACE(v_result_string, '<', '', '1', '1');
v_first_char_flag := 1;
ELSE
IF v_first_char_flag = 0 THEN
v_first_char := SUBSTR(v_result_string, 1, 1);
v_first_char_new := ''''||v_first_char;
v_result_string := REGEXP_REPLACE(v_result_string, v_first_char, v_first_char_new, 1);
v_first_char_flag := 1;
ELSE
v_result_string := REPLACE(v_result_string, '<', '''||');
v_counter := v_counter + 1;
v_flag := 1;
END IF;
END IF;
ELSE
v_result_string := REPLACE(v_result_string, '>','||''');
v_last_char := SUBSTR(v_result_string, LENGTH(v_result_string), 1);
v_last_char_new := v_last_char||'''';
v_result_string := REGEXP_REPLACE(v_result_string, v_last_char, v_last_char_new, LENGTH(v_result_string));
v_counter := v_counter + 1;
END IF;
END LOOP;
RETURN v_result_string;
END ReplaceStringFormat;
ORA-12725 appears at lines:
v_result_string := REGEXP_REPLACE(v_result_string, v_first_char, v_first_char_new, 1);
and
v_result_string := REGEXP_REPLACE(v_result_string, v_last_char, v_last_char_new, LENGTH(v_result_string));
Thanks for advice!
select replace('Employee <FIO> works in dept (<DEPTID>)','<FIO>', fio_column) from table_containing_fio_columnwould be all that is required. As many nested calls toreplaceare needed as you have placeholders.