TO_CHAR function & ORA-01722: invalid number error

Posted on 2014-12-01
Medium Priority
Last Modified: 2014-12-01

I receive ORA-01722: invalid number error when running this query.

select TO_CHAR(PRSPRSC/100/2,'fm999.00') NOW_PRICE from prs;

What is the best way around this?
Question by:xbox360dp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40474430
>> What is the best way around this?

Is PRSPRSC a number data type or a varchar2 that has numbers in it?

If a varchar2, the best way to fix it is fix the 'bad' data that is likely causing the problem.

The other choice is to write your own to_number function that can deal with non-numeric data the way you want to handle it and use that in the call.

If you create my_to_number then:

You would need to decide on what to return if the data is not a number.

Author Comment

ID: 40474470
PRSPRSC is a varchar2 that has numbers in it.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40474476
>>PRSPRSC is a varchar2 that has numbers in it.

then see the recommendations above:
1: Fix the data.
2: Write your own function to handle the 'bad' data.
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

LVL 74

Accepted Solution

sdstuber earned 2000 total points
ID: 40474511
I agree, writing your own function would probably be best, but as a temporary measure ,you could try a case.

TO_CHAR(case when regexp_like(PRSPRSC,'^[0-9]*\.?[0-9]+$') then PRSPRSC/100/2 end,'fm999.00')

this will cause your non-numeric strings to return null

alternately, put the conversion inside the case, and return the original string when it's not valid so it's easily visible

           WHEN REGEXP_LIKE(prsprsc, '^[0-9]*\.?[0-9]+$') THEN TO_CHAR(prsprsc / 100 / 2, 'fm999.00')
           ELSE prsprsc

note, these regexps aren't guaranteed to capture all weird data entry, but will probably work for most.
LVL 74

Expert Comment

ID: 40474558
As for a custom function, I would put all of the formatting into that rather than converting to number and then formatting.  That way you can encapsulate all of the rules nicely in one place.

    v_temp NUMBER;
    v_temp := TO_NUMBER(p_str);

    -- If a format is given use it, otherwise use the default format for numbers
    IF p_format IS NOT NULL
        RETURN TO_CHAR(v_temp, p_format);
        RETURN TO_CHAR(v_temp);
    END IF;
        RETURN p_str;  -- change this to whatever you want non-numerics to do.

Open in new window

and then usage would look like this...

select formatter(PRSPRSC/100/2,'fm999.00') NOW_PRICE from prs;
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40475027
The accepted solution is a temporary fix at best and as noted, has potential flaws moving forward in someone enters something that is 'close' to a number but not exactly a number.

If they do that, you are right back where you are now.

Something is allowing 'bad' data into your tables and can lead to very bad results depending on the application.

First thing you really need to do is stop storing numbers in a varchar2 column.  Varchar2 is for strings.  number is for numbers.

Since you likely cannot do that, I would look at a job/script that at least monitors the table for 'bad' data daily/weekly and fix it as it comes in.

Better approach is what I mentioned and sdstuber posted: your own function.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question