Solved

Oracle to_number gives error

Posted on 2012-03-27
8
905 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
[X]
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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

730 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