Solved

Oracle to_number gives error

Posted on 2012-03-27
8
909 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
Industry Leaders: 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!

 

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

Industry Leaders: 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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

691 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