ORA-01722 invalid number
Cause: The attempted
conversion of a character string to a number failed because the character
string was not a valid numeric literal. Only numeric fields or character fields
containing numeric data may be used in arithmetic functions or expressions.
Only numeric fields may be added to or subtracted from dates.
Action: Check the
character strings in the function or expression. Check that they contain only
numbers, a sign, a decimal point, and the character "E" or
"e" and retry the operation.
The
following sql successfully creates the
error, we are comparing a number
to a character.
SQL> SELECT 'X'
2
FROM DUAL
3
WHERE 1='A';
WHERE 1='A'
*
ERROR at line 3:
ORA-01722: invalid number
But it can get more
interesting as seen below. A number is inserted into a varchar2 column. In the
following sql’s , when we select the varchar2 column first and the number column next , the sql does
not fail. But if we do the other way round the sql fails as the where clause
checks for 999 in all the values for the varchar2 column. When it tries to
compare 999 to ‘STRING’ it fails.
This particular pattern
can cause the invalid number error especially when creating queryies dynamically,
or when we run reports depending on the parameters entered for a given report.
SQL> desc test
Name Null? Type
-----------------------------------------
-------- ----------------------------
LV_NUMBER NUMBER
LV_VARCHAR2 VARCHAR2(10)
SQL> select * from test;
LV_NUMBER LV_VARCHAR
----------
----------
1 STRING
2 999
1
select t.*
2
from test t
3
where lv_number=2 and
4*
lv_varchar2=999
SQL> /
lv_varchar2=999
*
ERROR at line 4:
ORA-01722: invalid number
1 select t.*
2
from test t
3
where lv_varchar2=999 and
4*
lv_number=2
5
/
LV_NUMBER LV_VARCHAR
---------- ----------
2 999