Dynamic PL/SQL & substr, instr function
I am having trouble with incorporating the SUBSTR and INSTR functions into my dynamic PL/SQL procedure using Oracle 8i.
I have data that is packed into one column seperated by a delimiter (':')
I need to seperate the data to use indicidual pieces.
If I run my query in general -
select substr(secondcol, 1, instr(secondcol, ':',1,1)-1) ONE,
substr(secondcol,instr(secondcol, ':',1,1)+1,instr(secondcol, ':',1,1)-1) TWO,
substr(secondcol,instr(secondcol, ':',1,2)+1,instr(secondcol, ':',1,1)-1) THREE,
substr(secondcol,instr(secondcol, ':',1,3)+1,instr(secondcol, ':',1,1)-1) FOUR
from temp_table where firstcol=100
This works and gives me the right result.
e.g
DATA :
Firstcol SECONDCOL
100 1:2:3:4
Result:
ONE TWO THREE FOUR
1 2 3 4
However to make this generic if I use it in a function passing it a parameter which has ':' delimited data it does not work and gives me errors. All I want is to get the output as a string that looks like my query above so I can use it in my proc.
create or replace function MYJUNK(TFieldNew IN CHAR)
RETURN CHAR IS
UpdateString Varchar2(100);
BEGIN
UpdateString := 'First=substr('||TFieldNew||', 1, instr('||TFieldNew||', '':'',1,1)-1) ONE, ';
UpdateString := UpdateString || ' Second=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,2)+1, instr('||TFieldNew||', '':'',1,1)-1) TWO, ';
UpdateString := UpdateString || ' third=substr('||TFieldNew||', instr('||TFieldNew||', '':'',1,3)+1, instr('||TFieldNew||', '':'',1,1)-1) THREE from temp_table';
return UpdateString;
END;
The function compiles but gives me run time errors
This is what I get -
SQL> select myjunk('''1:2:3:4''') from dual;
select myjunk('''1:2:3:4''') from dual
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SGHDTA.MYJUNK", line 8
ORA-06512: at line 1