Solved

Oracle to_number gives error

Posted on 2012-03-27
8
890 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 73

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 73

Expert Comment

by:sdstuber
ID: 37774411
select f1 from mytab1
where not REGEXP_LIKE(trim(f1), '^(\+|\-)?[0-9]*\.?[0-9]*$')
0
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 73

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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
levels for reporting 5 51
Has anyone used domo? 1 39
C# Error - Add Failed 12 49
Format column on datatable 7 17
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.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now