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
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 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.
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 .
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 .
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)?
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.
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.
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.
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
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With