Oracle SQL

Dear Experts,

 In Insert Query I got error message invalid number, How do I cast to zero if it an Invalid number

Thanks
Jamil MuammarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

HuaMin ChenProblem resolverCommented:
Hi,

If the number is having non-numeric characters inside, you can use a block of codes having Other exception to detect the error.

Otherwise, you can only use

To_number

Function to convert it.
0
slightwv (䄆 Netminder) Commented:
Is this insert statement already in a PL/SQL stored procedure or is it stand-alone from some other code?

I would look at creating your own function to return a zero if you give it invalid input.

Something like this:
create or replace function my_to_number(p_input in varchar2) return number is
junk number;
begin
	junk := to_number(p_input);
	return junk;

	exception when others then return 0;
end;
/


show errors


select my_to_number('Hello') from dual;
select my_to_number(12345) from dual;

Open in new window

0
Jamil MuammarAuthor Commented:
It's Oracle Database
0
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.

HuaMin ChenProblem resolverCommented:
Correction:

Use Others exception
0
awking00Information Technology SpecialistCommented:
Without function -
select case when regexp_instr(yourcolumn,'[^[:digit:]]') > 0 then 0 else to_number(yourcolumn) end as  yourcolumn
from yourtable;
0
Mark GeerlingsDatabase AdministratorCommented:
What does your insert statement look like?  Is it like #1 or #2 here?
1.  "insert into [table_name] [optional list of columns] values [list of values]
2. "insert into [table_name] [optional list of columns]  select ... from ...

For either type of "insert..." statement, you can use a function like the one slightwv suggested.

For #1, you will need to apply this function to each value in your "values" list that is going to a NUMBER column in the database.

For #2, the problem is bad data in one of the columns you are selecting, so you will need to use the function on each CHAR or VARCHAR2 column you are selecting that is being inserted into a NUMBER column.
0

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
HuaMin ChenProblem resolverCommented:
Details had been already given to resolve this thread!
0
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
Query Syntax

From novice to tech pro — start learning today.