I've created a cursor to select the actual data, and loop through it outputting the raw value and the value after it was converted to a number. The application occasionally throws invalid number errors. Below is my test (not including the select statement) code and the output.
LOOP
FETCH myCursor into v_answer;
EXIT WHEN myCursor%notfound;
DBMS_OUTPUT.PUT_LINE('Raw answer: ' || v_answer );
v_instr := INSTR(v_answer, '.',1 , 2) ;
v_number := TO_NUMBER(REPLACE(TRANSLATE (CASE v_instr
WHEN 0 THEN UPPER(v_answer)
ELSE 0
END,'ABCDEFGHIJKLMNOPQURSTWVXYZ+<>:',' '), ' ',''));
DBMS_output.put_line('As number: ' || v_number);
Here is the output:
Raw answer: 4
As number: 4
Raw answer: 3
As number: 3
Raw answer: 1.00
As number: 1
Raw answer: <3
I receive:
PL/SQL: numeric or value error: character to number conversion error
...when the Raw answer is '<3'.
Please note that the actual code used by the application looks like so:
AND TO_NUMBER(REPLACE(TRANSLATE ( decode( INSTR(hra_ans.answer_text, '.',1 , 2), 0 , UPPER(hra_ans.answer_text) , 0),'ABCDEFGHIJKLMNOPQURSTWVXYZ+<>:',' '), ' ',''))
and is part of the where clause in a dynamic sql string. I've replaced the decode statement with the case statement because I was getting an function or pseudo-column 'DECODE' may be used inside a SQL statement only error.
Finally, my questions are these:
- Why isn't the translate function replacing the less than sign and
- What is the difference (in layman's terms) between the ORA-1722 and ORA-06502 errors?
EDIT: I've noticed that when I change the case statement to:
CASE v_instr
WHEN 0 THEN UPPER(v_answer)
ELSE '0'
,
I no longer receive the 06502 error. From looking at the original code line I posted, are there any suggestions as to what may be causing the invalid number error (assuming that no characters exist in the string to be translated are not accounted for)? Or, is there a better way to accomplish what the original developer was attempting to do?
Here are the variable declarations:
v_answer varchar2(2000);
v_number number;
v_instr number;