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

i have this column MILLARES varchar2(50)

storage this values:

i need convert only two decimal, i use to_number but i have error: ORA-01722: invalid number:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ".,":

Open in new window

PortletPaulEE Topic AdvisorCommented:
try these:
      , to_number(MILLARES)
where upper(MILLARES) = lower(MILLARES)

-- non-numeric values
where upper(MILLARES) <> lower(MILLARES)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wasim Akram ShaikCommented:
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!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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Geert GOracle dbaCommented:
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
johnsoneSenior Oracle DBACommented:
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)
   v_new_num NUMBER;
   v_new_num := TO_NUMBER(p_string);
   RETURN 1;
   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.
awking00Information Technology SpecialistCommented:
To find non-numeric characters -
select * from table where regexp_replace(millares,'[0-9.]+') is not null;
awking00Information Technology SpecialistCommented:
Note that the upper/lower comparison will not catch special characters like '#' or a linefeed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.