Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TO_NUMBER not throwing exceptions on invalid input.

user12204556Oct 6 2010 — edited Oct 6 2010
I am having problems safely converting text values to numbers with the TO_NUMBER function.
select to_number('BAD') from dual
Result: ORA-01722: invalid number. This makes sense.
select to_number('6E9547') from dual
Result: ORA-01722: invalid number. This makes sense too considering that the E is treated as an exponent marker.

So I create a function to test if a number is valid:
CREATE OR REPLACE FUNCTION is_number( str IN VARCHAR2 ) RETURN NUMBER IS
    BEGIN
     RETURN TO_NUMBER(str);
    EXCEPTION
     WHEN OTHERS THEN RETURN NULL;
    END;
Now it is time for a retest:
select is_number('BAD') from dual
Result: NULL as expected
select is_number('6E9547') from dual
Result: 1E126
This last one is unexpected. I expect NULL but get a 'random' number?

All this on 10.2.0.4 (patch set 20) on Windows 2003 server.
This post has been answered by MichaelS on Oct 6 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2010
Added on Oct 6 2010
16 comments
8,357 views