?
Solved

Oracle to_number gives error

Posted on 2012-03-27
8
Medium Priority
?
912 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit 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 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, we’ll look at how to deploy ProxySQL.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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.
Suggested Courses

800 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