I have this SQL query:
SELECT *
FROM coa_cook
WHERE grt_tOOK_ID IN (301173, 301202)
and grt_tOOK_ID is of varchar2 data type in the table.
I am getting this error :
ORA-01722: invalid number
Someone has told me to put quotes between them as it is varchar2 data type please advise
The column is a varchar (not a number). You could use
SELECT * FROM coa_cook WHERE grt_tOOK_ID IN ('301173','301202')
or
SELECT * FROM coa_cook WHERE TO_NUMBER(grt_tOOK_ID) IN (301173,301202)
Related
I created sequence for manager first.
CREATE SEQUENCE manager_seq
START WITH 200
MAXVALUE 299
CYCLE
CACHE 10;
Then I inserted some values using .NEXTVAL
INSERT INTO manager
VALUES (manager_seq.NEXTVAL,'')
When I try to query with where statement it says ORA-00936: missing expression
select * from manager where number = 201;
Why it isn't working with sequnce numbers how can I use them?
Column name can't be number, that's a reserved word - reserved for datatype. For example:
SQL> create table manager (id number,
2 number number); --> this
number number)
*
ERROR at line 2:
ORA-00904: : invalid identifier
Therefore, post table description (so that we could suggest what to do) or use valid query.
If you use a reserved word as an identifier then you need to quote it everywhere you use it. So, if you have the table:
CREATE TABLE manager (
"NUMBER" NUMBER,
something VARCHAR2(10)
);
Then you insert rows:
INSERT INTO manager VALUES (manager_seq.NEXTVAL,'');
INSERT INTO manager VALUES (manager_seq.NEXTVAL,'');
Then, if you use an unquoted identifier:
select * from manager where number = 201;
You get the error:
ORA-00936: missing expression
But, if you use a quoted identifier with the correct case then you can query the table:
select * from manager where "NUMBER" = 201;
Which outputs:
NUMBER
SOMETHING
201
null
Note: In Oracle, '' and NULL are identical.
db<>fiddle here
I am trying to use range partition on a table for a column of type number. However, Oracle 12c throws an error saying it is an invalid data-type. I don't understand why/what is invalid in the command.
create table partition_tester (
some_column number not null,
another_column varchar2(10),
partition by range(some_column) interval(10)
);
I am connecting to the database using SQL developer. It seems to have issue with the range function. On executing above script it throws:
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Any help is appreciated.
remove the comma after " another_column varchar2(10)," and specify at least one partition.
create table partition_tester (
some_column number not null,
another_column varchar2(10))
partition by range(some_column) interval(10)
(
partition p0 values less than (10)
)
I encountered this scenario where the insert based on '' on number column works fine while the case statement gives different error.
"inconsistent datatypes: expected %s got %s"
I have gone through some of the explanation given here (ORA-00932 inconsistent datatypes expected char got number ) and Oracle SQL CASE WHEN ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent datatypes: expected %s got %s" and I understand that I need to use it without quotes but I wanted to understand the behavior of case statement. SQL server does not behave like this, but why only oracle. Is this oracle default behavior with case statement.
If I create and insert values, I can do it without any error.
create table testtbl (name varchar2(50), id number(10,2));
insert into testtbl values ('abc', '123');
insert into testtbl values ('test', '200');
When I try to update the column using case statement I am getting this error. I can use to_number or remove the quotes to avoid this error but wanted to understand why it happens with quotes when the column given in the else statement is number itself. Doesn't implicit conversion exist in Oracle?
update testtbl
set id = case when name = 'abc' then '5000' else id end;
Doesn't implicit conversion exist in Oracle?
It does. See documentation eg here.
Is this oracle default behavior with case statement.
Yes. From the documentation:
For both simple and searched CASE expressions, all of the return_exprs must either have the same datatype (CHAR, VARCHAR2, NCHAR, or NVARCHAR2, NUMBER, BINARY_FLOAT, or BINARY_DOUBLE) or must all have a numeric datatype.
Oracle throws ORA-01722: invalid number in my SQL query and it is unclear why.
I have a table called "LIGHTS" and I want to get the lights with a WATTAGE <= 3. WATTAGE is stored as a VARCHAR2(40) for some reason, but each character does seem to be an integer or float. When I convert WATTAGE to a number using the query:
SELECT TO_NUMBER(WATTAGE) FROM LIGHTS
There's no problem. I get a result like this:
TO_NUMBER(WATTAGE)
1
7
-1
0
15
17.5
However, when I add a WHERE condition to filter the numbers for those less than 3, I get the ORA-01722: invalid number error:
SELECT WATTAGE FROM LIGHTS
WHERE TO_NUMBER(WATTAGE) <= 3
What could be going wrong?
ORA-01722: invalid number comes from the TO_NUMBER(), not from the conditional. I.e., try this and you'll get the same error:
SELECT TO_NUMBER('test') FROM dual;
This would indicate that at least one of your values is not numeric.
Alas Oracle doesn't have a simple way to check whether a string is in fact representing a number. (One of the many reasons to use the correct data type in the first place!)
However, you can write your own. Here is just a brief demo of this concept. I create a table with a column of VARCHAR2 data type, and populate it with a few strings, one of which is not a number.
create table tbl (nbr varchar2(100));
insert into tbl
select '103' from dual union all
select '-1.3' from dual union all
select 'abc' from dual
;
Then I create a small function with a nested block that should error out if TO_NUMBER fails. The error handler will "do something" specific to errors and then return control to the main function. Then I can use this in a WHERE clause. Here are the function and then how it can be used to find the offending values:
create or replace function not_a_number(str varchar2)
return varchar2
as
x number;
r varchar2(100);
begin
begin
x := to_number(str);
exception
when others then
r := str;
end;
return r;
end;
/
select nbr
from tbl
where not_a_number(nbr) is not null;
NBR
-------
abc
In oracle DB I have a table TABLE1 with column datatype defined as VARCHAR2(50). I have another table TABLE2 with column datatype defined as a NUMBER. The no of rows present in the TABLE1 is around 23k and they all seems to be number. But when I try the insert command I get the below error.
Error report -
SQL Error: ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
This clearly means that there are few rows that are not numbers. But I am not able to identify those no numeric cases. How do I identify the non numeric rows.
P.S:
Oracle database version : 9.2.0.8.0
And yes I know its ancient.
Why are you storing numbers in a string? That is the fundamental problem.
You can avoid the rows using a where clause:
insert into table2( . . .)
select . . .
from table1 t1
where regexp_like(numbercol, '^[0-9]+$')
This assumes that "number" means "non-negative integer". Obviously the pattern can be generalized for other ranges and/or numeric formats.
You can just identify the non-numeric rows by running a similar select:
select . . .
from table1 t1
where not regexp_like(numbercol, '^[0-9]+$')
Below is the PL SQL block used to identify the discrepancy cases. The special character in my case was double quotes. There were few cases with issue so printing the output was feasible. If there are more number of discrepancy cases then capturing the error msg in a table column will be helpful.
declare
cursor cur is
select * from TABLE1 ; ----- cursor to loop through cases individually
begin
for rec in cur
loop
begin
insert into table2 (column1)
select to_number(column1)
from table1
where
column1=rec.column1 ;
exception when others
then
Dbms_Output.Put_Line('error for '||rec.column1||' '||substr(sqlerrm,0,150)); --- error msg with discrepancy case
end;
end loop;
end;
/