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)
)
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 have the below query
select avg(to_number(pst_adm_scr)) from dmereg01.tst_scr
I keep getting the error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The field is originally a varchar with numeric values (scores)
Depending on your database version (which you haven't specified), you may be able to use the ON CONVERSION ERROR component of TO_NUMBER to ignore the invalid values
SELECT TO_NUMBER('-' DEFAULT null ON CONVERSION ERROR)
from dual;
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.
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;
/
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)