Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-01722: invalid number in to_number

Tags:

sql

oracle

I’m trying to connect data from two tables by invoice number,

LOC         NOT NULL VARCHAR2(40)
INV_ORG            NUMBER

Like this it works fine:

SELECT     oh.inv_org,e.loc
FROM       headers oh, sites e
WHERE
AND e.location = TO_CHAR(oh.inv_org);

But when I try to convert char to number I get ORA-01722: invalid number

SELECT     oh.inv_org, e.loc
FROM       headers oh, sites e
WHERE
AND to_number(e.loc) = oh.inv_org;

Error report

ORA-01722: invalid number

I don't know why because in select, conversion works also fine

INV_ORG  110992
LOC  110992
OH.INV_ORG-TO_NUMBER(E.LOC) 0
like image 767
Bart Avatar asked Aug 20 '18 06:08

Bart


People also ask

How do I fix Ora-01722 Invalid number?

In the event of using INSERT or UPDATE to supply values to a sub query, the ORA-01722 will be hidden. You will have to find the row that is supplying an invalid numerical string and resolve it. If instead of INSERT you attempt SELECT, the error will derive from an implicit conversion in the WHERE clause.

What does invalid number mean in Oracle?

What Is the Invalid Number Error? The invalid number error happens when Oracle attempts to convert a string to a number field but can't. This is often because the supplied string value is not a number (e.g. it is a letter or punctuation character). You'll get an error like this in your output: ORA-01722: invalid number.

What does TO_NUMBER mean in SQL?

TO_NUMBER converts expr to a value of NUMBER datatype. The expr can be a BINARY_FLOAT or BINARY_DOUBLE value or a value of CHAR , VARCHAR2 , NCHAR , or NVARCHAR2 datatype containing a number in the format specified by the optional format model fmt .

What does To_char do in SQL?

TO_CHAR (datetime) converts a datetime or interval value of DATE , TIMESTAMP , TIMESTAMP WITH TIME ZONE , or TIMESTAMP WITH LOCAL TIME ZONE datatype to a value of VARCHAR2 datatype in the format specified by the date format fmt .

What does error ora-01722 invalid number mean?

HiThe "ORA-01722 invalid number error" usually occurs because of conversion. (ex: converting a string to number).Have you used any expression or function in the query.Check with the datatype too. Do you have a date field (date data type)?

Why do I get an invalid number error in Oracle?

You may also need to Trim your fields before sending them to Oracle to help with this if your source is a Sequential file.-craig HiThe "ORA-01722 invalid number error" usually occurs because of conversion. (ex: converting a string to number).Have you used any expression or function in the query.Check with the datatype too.

How to resolve the ora-1722 error in Python?

I have given the basic scenarios of producing the ORA-1722 error.This error will come when we tries to convert the string value in to number using to_number function. The string can not be converted to number.So to resolve the error we need to use number value in to_number function.

How to fix “invalid number” error?

The solution to the “invalid number” error message could be one of several things: Are you trying to INSERT data into a table using INSERT INTO VALUES? If so, check that your columns are aligned to your values. I mean, make sure that the position of the columns that contain numbers match the numbers you’re trying to insert.


2 Answers

Check your data, it's possible to have a value in e.loc cannot be converted to number.

By the way, below select gave ORA-01722:

SELECT to_number('test') FROM dual

like image 91
Adrian Tufa Avatar answered Oct 10 '22 01:10

Adrian Tufa


If this doesn't work:

SELECT     oh.inv_org, e.loc
FROM       headers oh, sites e
WHERE
AND to_number(e.loc) = oh.inv_org;

it means that there are e.loc values that can't be converted to numbers (such as letters). Therefore, you should use query which works (as there's no problem in converting oh.inv_org to a string):

SELECT     oh.inv_org,e.loc
FROM       headers oh, sites e
WHERE
AND e.location = TO_CHAR(oh.inv_org);

However, the whole story means that current data model isn't correctly set. Columns you are joining might need to be used in a foreign key constraint of some kind, i.e.

  • there should be a table that contains locations,
  • it should have a primary key, and
  • that primary key column should be referenced from both headers and sites tables.

If that was the case, you wouldn't have problems you have now.

P.S. Oh, yes - and, try to switch to ANSI joins; won't fix your current problems, but will pay off in the future.

SELECT oh.inv_org,e.loc
FROM   headers oh join sites e on e.location = to_char(oh.inv_org);
like image 4
Littlefoot Avatar answered Oct 10 '22 01:10

Littlefoot