?
Solved

Oracle to_number gives error

Posted on 2012-03-27
8
Medium Priority
?
924 Views
Last Modified: 2012-04-11
UPDATE mytab1 t1
         SET t1.f1 = NVL((SELECT MAX(TO_CHAR((CASE
                                                      WHEN REGEXP_INSTR(TRIM(f1),
                                                                        '^[0-9]{0,180}(|\.[0-9]{1,2})$' ) = 1 THEN
                                                       TO_CHAR(TO_NUMBER(TRIM(f1)),
                                                               'FM9999.00')  
                                                      ELSE
                                                       NULL
                                                    END)))
                                   FROM t2
                                  WHERE id = t1.id ),
                                 '(None)');

I have problem with the above query in Oracle 11gR2. This query was running without issues but now it is giving error:  ORA-01722: invalid number.
The error is coming from the 5th line above: TO_NUMBER(TRIM(f1))

Is there any way to find out the offending contents of TRIM(f1) that cannot be used with TO_NUMBER ? If I write an exception block -- even there how do I get the specific value of TRIM(f1) for which the above update query fails.

Typical values of t2.f1 are:
2.0
.255
54
  33.5678
a_text_value
31.22

etc.

The corresponding outputs are:
2.00
.25
54.00
33.56

31.22

I get the above output (used to get) except I am now getting this exception.....
It seems WHEN REGEXP_INSTR(TRIM(f1), '^[0-9]{0,180}(|\.[0-9]{1,2})$' ) = 1
The above is not enough to validate a number.

Thank you.
0
Comment
Question by:toooki
  • 4
  • 4
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 37774323
try this...


WHEN REGEXP_LIKE(trim(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$') THEN
0
 

Author Comment

by:toooki
ID: 37774397
Thank you.
I tried .. But I get the same error: ORA-01722: invalid number.
UPDATE mytab1 t1
         SET t1.f1 = NVL((SELECT MAX(TO_CHAR((CASE
                                                      WHEN REGEXP_LIKE(trim(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$') THEN
                                                       TO_CHAR(TO_NUMBER(TRIM(f1)),
                                                               'FM9999.00')  
                                                      ELSE
                                                       NULL
                                                    END)))
                                   FROM t2
                                  WHERE id = t1.id ),
                                 '(None)');
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37774411
select f1 from mytab1
where not REGEXP_LIKE(trim(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$')
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:toooki
ID: 37774574
The above query returns strings. The o/p I get is like this:
none
na
mystring

It still returns too many records to find the possible offending one.
If there were any non-numeric characters in the field, it used to return null before (that is what was intended).

Thank you.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 37774609
you had an extra conversion in there, maybe that's what was messing it up


UPDATE t1
   SET f1 =
           NVL(
               (SELECT MAX(
                           (CASE
                                WHEN REGEXP_LIKE(TRIM(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$')
                                THEN
                                    TO_CHAR(TO_NUMBER(TRIM(f1)), 'FM9999.00')
                                ELSE
                                    ''
                            END)
                       )
                  FROM t2
                 WHERE id = t1.id),
               '(None)'
           );
0
 

Author Comment

by:toooki
ID: 37775022
Thanks a lot.

Actually I made a small change..
Instead of TRIM(f1)
I used
REGEXP_LIKE(TRIM(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$')

In two places above. You told me long ago to use the above to get rid of leading/trailing multiple blank characters from string. I kept the working query ... and the above worked..

Thank you.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37775037
I'm not sure where you mean.

TRIM(f1) which returns a string  

 is not compatible with

REGEXP_LIKE(TRIM(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$')  which is a boolean condition
0
 

Author Comment

by:toooki
ID: 37835261
Thank you very much. It worked after that and so I did not dig into that...
Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Via a live example, show how to take different types of Oracle backups using RMAN.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

809 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