SQL error "ORA-01722: invalid number"
Asked Answered
C

18

140

A very easy one for someone, The following insert is giving me the

ORA-01722: invalid number

why?

INSERT INTO CUSTOMER VALUES (1,'MALADY','Claire','27 Smith St Caulfield','0419 853 694');
INSERT INTO CUSTOMER VALUES (2,'GIBSON','Jake','27 Smith St Caulfield','0415 713 598');
INSERT INTO CUSTOMER VALUES (3,'LUU','Barry','5  Jones St Malvern','0413 591 341');
INSERT INTO CUSTOMER VALUES (4,'JONES','Michael','7  Smith St Caulfield','0419 853 694');
INSERT INTO CUSTOMER VALUES (5,'MALADY','Betty','27 Smith St Knox','0418 418 347');
Corfam answered 23/9, 2012 at 1:24 Comment(7)
So... what's the table definition for CUSTOMER? You've only given half the information needed.Equidistant
The telephone numbers are the only thing which might reasonably be a defined as a numeric which your data doesn't represent as a numeric (spaces aren't numeric). So: check your table definition and compare with your input statements.Havoc
Why would people down vote this question. For people who are new to databases, this is a weird error. I can see how enclosing the values with quotes might make it look like it's a string. It just depends on what the database is setup as. It might all be strings or numbers just depends on the fields. Maybe it was an error when the database was created.Aricaarick
An Example where the above situation comes: insert into table_1 (rollNumber) values ('123'); where rollNumber is a column of type "number".Hyperbolize
"Back in o`twelve, I pulled a book off the shelve, and answered an Oracle question. At 'Stack I'm still adept, up with knowledge I've kept, but I still don't have an accept."Chigoe
Telephone numbers shouldn't be defined as numeric, not to lose the leading zeroes.Tabu
Possible duplicate of Invalid number in Oracle SQL CaseAssibilate
C
161

An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a number.

Without seeing your table definition, it looks like you're trying to convert the numeric sequence at the end of your values list to a number, and the spaces that delimit it are throwing this error. But based on the information you've given us, it could be happening on any field (other than the first one).

Chigoe answered 23/9, 2012 at 1:32 Comment(3)
Also notice that manually complete a field with "(null)" will give you that error. If the defaul is null and you don't complete it will auto-complete with (null) but it is not the same when you type it.Uranology
Eh, "the string cannot be converted into a number" - It is not the point. The String CAN be converted to number. The point here is the opposite conversion of a number to a String column, but Oracle does not do that automatically: You have to make it a String yourself, marking the value explicitly in your SQL expression, to surround it by ' ' OR " ".Elf
To help troubleshoot (to narrow down which particular value in your SQL statement it is that the error refers to), it can be helpful to note the character position (if that is also outputted in the error message). For example, Position: 100 means the problematic value is 100 characters from the beginning of the attempted SQL statement.Elora
K
32

Suppose tel_number is defined as NUMBER - then the blank spaces in this provided value cannot be converted into a number:

create table telephone_number (tel_number number);
insert into telephone_number values ('0419 853 694');

The above gives you a

ORA-01722: invalid number

Keelia answered 23/9, 2012 at 8:37 Comment(0)
H
22

Here's one way to solve it. Remove non-numeric characters then cast it as a number.

cast(regexp_replace('0419 853 694', '[^0-9]+', '') as number)
Housetop answered 27/12, 2013 at 15:35 Comment(2)
Doing this would remove the leading 0.Scouring
this goes to original OP - your table column cant be of type "number" if you want to store values like '0419 853 694' because number cannot have leading zeroes. in my case however this is just what i needed, ty gmlacrosse!Fanestil
K
14

This is because:

You executed an SQL statement that tried to convert a string to a number, but it was unsuccessful.

As explained in:

To resolve this error:

Only numeric fields or character fields that contain numeric values can be used in arithmetic operations. Make sure that all expressions evaluate to numbers.

Kacikacie answered 23/9, 2012 at 1:31 Comment(1)
This can also happen if trying to insert a number into a varchar field.Oleneolenka
C
14

Well it also can be :

SELECT t.col1, t.col2, ('test' + t.col3) as test_col3 
FROM table t;

where for concatenation in oracle is used the operator || not +.

In this case you get : ORA-01722: invalid number ...

Capacitate answered 8/8, 2016 at 12:35 Comment(1)
Thats a good one. Another poisonous destiny may be this. You commented out a field in a SELECT Fieldlist like -- t.fieldname. Later you intend to remove that line start comment, but one commented character is left by accident. Thus there is: - t.fieldname. This happened to me, when I was working on a very large query, where I have to comment/uncommment hundreds of colums, while reorganizing the beast.Incombustible
B
10

As this error comes when you are trying to insert non-numeric value into a numeric column in db it seems that your last field might be numeric and you are trying to send it as a string in database. check your last value.

Bastinado answered 23/9, 2012 at 3:10 Comment(0)
E
8

Oracle does automatic String2number conversion, for String column values! However, for the textual comparisons in SQL, the input must be delimited as a String explicitly: The opposite conversion number2String is not performed automatically, not on the SQL-query level.

I had this query:

select max(acc_num) from ACCOUNTS where acc_num between 1001000 and 1001999;

That one presented a problem: Error: ORA-01722: invalid number

I have just surrounded the "numerical" values, to make them 'Strings', just making them explicitly delimited:

select max(acc_num) from ACCOUNTS where acc_num between '1001000' and '1001999';

...and voilà: It returns the expected result.

edit: And indeed: the col acc_num in my table is defined as String. Although not numerical, the invalid number was reported. And the explicit delimiting of the string-numbers resolved the problem.

On the other hand, Oracle can treat Strings as numbers. So the numerical operations/functions can be applied on the Strings, and these queries work:

select max(string_column) from TABLE;

select string_column from TABLE where string_column between '2' and 'z';

select string_column from TABLE where string_column > '1';

select string_column from TABLE where string_column <= 'b';

Elf answered 15/11, 2017 at 12:8 Comment(1)
i encountered a similar situation. ora-01722 invalid-number was triggered at a select statement, not an insert statement. I checked the table definition and found that the column was a varchar instead of number, so i added single quotes around the numberBraeunig
N
3

In my case the conversion error was in functional based index, that I had created for the table.

The data being inserted was OK. It took me a while to figure out that the actual error came from the buggy index.

Would be nice, if Oracle could have gave more precise error message in this case.

Naidanaiditch answered 2/9, 2014 at 14:28 Comment(0)
V
2

If you do an insert into...select * from...statement, it's easy to get the 'Invalid Number' error as well.

Let's say you have a table called FUND_ACCOUNT that has two columns:

AID_YEAR  char(4)
OFFICE_ID char(5)

And let's say that you want to modify the OFFICE_ID to be numeric, but that there are existing rows in the table, and even worse, some of those rows have an OFFICE_ID value of ' ' (blank). In Oracle, you can't modify the datatype of a column if the table has data, and it requires a little trickery to convert a ' ' to a 0. So here's how to do it:

  1. Create a duplicate table: CREATE TABLE FUND_ACCOUNT2 AS SELECT * FROM FUND_ACCOUNT;
  2. Delete all the rows from the original table: DELETE FROM FUND_ACCOUNT;
  3. Once there's no data in the original table, alter the data type of its OFFICE_ID column: ALTER TABLE FUND_ACCOUNT MODIFY (OFFICE_ID number);

  4. But then here's the tricky part. Because some rows contain blank OFFICE_ID values, if you do a simple INSERT INTO FUND_ACCOUNT SELECT * FROM FUND_ACCOUNT2, you'll get the "ORA-01722 Invalid Number" error. In order to convert the ' ' (blank) OFFICE_IDs into 0's, your insert statement will have to look like this:

INSERT INTO FUND_ACCOUNT (AID_YEAR, OFFICE_ID) SELECT AID_YEAR, decode(OFFICE_ID,' ',0,OFFICE_ID) FROM FUND_ACCOUNT2;

Vivavivace answered 23/9, 2015 at 22:23 Comment(1)
This is a very complicated example of a case where the error could occur + an explanation how to solve that specific case, which may not apply at all.Olaolaf
W
1

I have found that the order of your SQL statement parameters is also important and the order they are instantiated in your code, this worked in my case when using "Oracle Data Provider for .NET, Managed Driver".

var sql = "INSERT INTO table (param1, param2) VALUES (:param1, :param2)";
...
cmd.Parameters.Add(new OracleParameter("param2", Convert.ToInt32("100")));
cmd.Parameters.Add(new OracleParameter("param1", "alpha")); // This should be instantiated above param1.

Param1 was alpha and param2 was numeric, hence the "ORA-01722: invalid number" error message. Although the names clearly shows which parameter it is in the instantiation, the order is important. Make sure you instantiate in the order the SQL is defined.

Wailoo answered 16/3, 2022 at 13:38 Comment(0)
U
1

For me this error was a bit complicated issue.

I was passing a collection of numbers (type t_numbers is table of number index by pls_integer;) to a stored procedure. In the stored proc there was a bug where numbers in this collection were compared to a varchar column

select ... where ... (exists (select null from table (i_coll) ic where ic.column_value = varchar_column))

Oracle should see that ic.column_value is integer so shouldn't be compared directly to varchar but it didn't (or there is trust for conversion routines).

Further complication is that the stored proc has debugging output, but this error came up before sp was executed (no debug output at all).

Furthermore, collections [<empty>] and [0] didn't give the error, but for example [1] errored out.

Unsavory answered 5/9, 2022 at 12:43 Comment(0)
M
0

The ORA-01722 error is pretty straightforward. According to Tom Kyte:

We've attempted to either explicity or implicity convert a character string to a number and it is failing.

However, where the problem is is often not apparent at first. This page helped me to troubleshoot, find, and fix my problem. Hint: look for places where you are explicitly or implicitly converting a string to a number. (I had NVL(number_field, 'string') in my code.)

Marrissa answered 11/5, 2016 at 23:1 Comment(0)
M
0

This happened to me too, but the problem was actually different: file encoding.

The file was correct, but the file encoding was wrong. It was generated by the export utility of SQL Server and I saved it as Unicode.

The file itself looked good in the text editor, but when I opened the *.bad file that the SQL*loader generated with the rejected lines, I saw it had bad characters between every original character. Then I though about the encoding.

I opened the original file with Notepad++ and converted it to ANSI, and everything loaded properly.

Manslaughter answered 12/3, 2020 at 14:54 Comment(0)
S
0

In my case it was an end of line problem, I fixed it with dos2unix command.

Seal answered 25/10, 2022 at 22:59 Comment(0)
S
0

In my case I was trying to Execute below query, which caused the above error ( Note : cus_id is a NUMBER type column)

select * 
from customer a
where a.cus_id IN ('115,116')

As a solution to the caused error, below code fragment(regex) can be used which is added in side IN clause (This is not memory consuming as well)

select * 
from customer a
where a.cus_id IN (select regexp_substr (
       com_value,
       '[^,]+',
       1,
       level
     ) value
from (SELECT '115,116' com_value
            FROM dual)rws
connect by level <= 
length ( com_value ) - length ( replace ( com_value, ',' ) ) + 1)
Stowage answered 23/12, 2022 at 3:21 Comment(0)
A
0

SQL error "ORA-01722: invalid number"

Normally this issue you faced over test env the query which is working fine over test env will create issue over prod due to version control issue. In case the below query was working over test env accurately.

  1. Test ENV.

SELECT * FROM FBS.TSPTable WHERE RTCODE=230 AND ID =23431839941 AND CREATION_DATE = (SELECT MAX (CREATION_DATE) FROM TSPTable WHERE RTCODE = 230 AND ID=23431839941)

Qry Result Successful over test env.
  1. Over PROD.

SELECT * FROM FBS.TSPTable WHERE RTCODE=230 AND ID =23431839941 AND CREATION_DATE = (SELECT MAX (CREATION_DATE) FROM TSPTable WHERE RTCODE = 230 AND ID =23431839941)

Error:java.sql.SQLSyntaxErrorException: ORA-01722: invalid number

3.How I solved this ISSUE by Adding Colon to the parameter values as belowThis is strange i fixed it by addin colon as below .

SELECT * FROM FBS.TSPTable WHERE RTCODE='230' AND ID ='23431839941' AND CREATION_DATE = (SELECT MAX (CREATION_DATE) FROM TSPTable WHERE RTCODE = '230' AND ID ='23431839941')

And get the success result over the PROD, the same query work on both env well.

Angelineangelique answered 14/9, 2023 at 9:41 Comment(0)
V
-1

try this as well, when you have a invalid number error

In this a.emplid is number and b.emplid is an varchar2 so if you got to convert one of the sides

where to_char(a.emplid)=b.emplid

Vernitavernoleninsk answered 8/6, 2016 at 14:35 Comment(0)
S
-4

You can always use TO_NUMBER() function in order to remove this error.This can be included as INSERT INTO employees phone_number values(TO_NUMBER('0419 853 694');

Scenarist answered 11/10, 2014 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.