Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PLSQL: ORA-01722: invalid number: MILLARES varchar2(50)

Posted on 2014-10-30
7
Medium Priority
?
521 Views
Last Modified: 2014-10-31
i have this column MILLARES varchar2(50)

storage this values:
284.69299999999998
0
244.327
371.53199999999998
975
9.0850000000000009

i need convert only two decimal, i use to_number but i have error: ORA-01722: invalid number:
0
Comment
Question by:enrique_aeo
[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
7 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 40415020
It works
Most likely it does not work for you due to the NLS_LANG settings. Can you check what format is currently set. Check this article to find out. If required, change it to ".,":
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ".,";

Open in new window

0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1400 total points
ID: 40415030
try these:
select
        MILLARES 
      , to_number(MILLARES)
from THETABLE 
where upper(MILLARES) = lower(MILLARES)
;

-- non-numeric values
select
        MILLARES 
from THETABLE 
where upper(MILLARES) <> lower(MILLARES)
;

Open in new window

0
 
LVL 16

Assisted Solution

by:Wasim Akram Shaik
Wasim Akram Shaik earned 200 total points
ID: 40415052
One more reason for that error could be, the values which are stored in the table may have an appended extra line character in their value..

see the same sql fiddle posted here to recreate your error

http://sqlfiddle.com/#!4/2fc7b/1

The error generated because of this value

insert into Table1 ("MILLARES")
		 VALUES ('  9.0850000000000009 
                 ');

Open in new window



See the New Line Character , it may not be visible to you but while internal conversion from char to number, this may have raised the error which you have encountered

In your case you may have some hidden text characters if not exactly a new line character, you have to find them out and and replace them with null so as to proceed further without any error

for ex: The below statement will result in the ORA-01722: invalid number:

select to_number('  9.0850000000000009 
                 ') from dual;

Open in new window


Where as the below statement will execute successfully


select to_number(replace('  9.0850000000000009 
                 ',chr(10),'')) from dual;

Open in new window


You can use round function in addition to the above one so as to round the value to 2 decimal points
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.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 40415186
the column is a  varchar type ...

try and find all data which isn't a number first
exclude that and then run to_number

with cte as (
  select *
  from table_name
  where regexp_like(MILLARES , '\d*([.,]\d*)?'))
select to_number(millares) num_millares from cte

and then trunc it to 2 decimals
with cte as (
  select *
  from table_name
  where regexp_like(MILLARES , '\d*([.,]\d*)?'))
select trunc(to_number(millares), 2) num_millares from cte
0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 200 total points
ID: 40415447
Along the lines of everyone else, I would believe that this is a data issue.

To find the bad rows, you can use regexp as suggested, however that wouldn't account for difference in NLS settings.  I would prefer to use a function like this one:

CREATE FUNCTION is_number (p_string IN VARCHAR2)
   RETURN INT
IS
   v_new_num NUMBER;
BEGIN
   v_new_num := TO_NUMBER(p_string);
   RETURN 1;
EXCEPTION
WHEN VALUE_ERROR THEN
   RETURN 0;
END is_number;

Open in new window


Then to find the bad records, it would simply be this:

SELECT rowid, millares 
FROM   mytab 
WHERE  Is_number(millares) = 0; 

Open in new window


From there, fix the rows that show in the query and then your original issue should go away.  However, it will come back as soon as bad data enters the field again.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 200 total points
ID: 40415804
To find non-numeric characters -
select * from table where regexp_replace(millares,'[0-9.]+') is not null;
0
 
LVL 32

Expert Comment

by:awking00
ID: 40416077
Note that the upper/lower comparison will not catch special characters like '#' or a linefeed.
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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

721 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