getting invalid number when aggregating varchar using to_number - sql

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;

Related

Invalid datatype for range function

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)
)

Oracle differences using Quotes in case statement and insert

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.

ORA-00904 while converting UTC date to Oracle date

I wrote the following query in Oracle, and it is working fine in one schema but giving an ORA-00904 error for the utc_to_oracle converter function in another schema.
SELECT MIN(to_char(utc_to_oracle(start_t),'DD-MON-YYYY hh24:mi:ss')),
MAX(to_char(utc_to_oracle(end_t),'DD-MON-YYYY hh24:mi:ss'))
FROM ts.udata
WHERE entity_type='wxyz'
Error:
ORA-00904: "UTC_TO_ORACLE": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 2 Column: 13
Why is it working on one schema but not the other?
ORA-00904 occurs when you reference an invalid object.
You need to provide grants for the user defined object UTC_TO_ORACLE to the target user to have this executed.
GRANT SELECT ON UTC_TO_ORACLE TO XXX;

Getting error invalid number in sql query

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)

ORA-00904 when inserting/querying data from tables

I'm getting the flowing error:
Error starting at line 1 in command:
INSERT INTO driver (registration, make, model, gvw, year, body) VALUES('4585 AW','ALBION','RIEVER',20321,1963, ' ');
Error at Command Line:1 Column:53
Error report:
SQL Error: ORA-00904: "BODY": invalid identifier
00904. 00000 - "%s: invalid identifier"
When I do the following
INSERT INTO driver
(registration, make, model, gvw, year)
VALUES
('4585 AW','ALBION','RIEVER',20321,1963, ' ');
So I temporally deleted the body data and then give the error
Error starting at line 1 in command:
INSERT INTO driver (registration, make, model, gvw, year) VALUES('4585 AW','ALBION','RIEVER',20321,1963)
Error at Command Line:1 Column:53
Error report:
SQL Error: ORA-00904: "YEAR": invalid identifier
00904. 00000 - "%s: invalid identifier"
I have columns both called year and body, yet I'm getting errors. How can I fix this?
Seems like you specified the column names under quotes in your DDL.
Oracle considers identifiers under quotes as a case sensitive, so creating a table with a column name as "body" or "year" will not resolve body or year in your query, because when you omit the quotes, it considers the upper case variant.
So when you have this:
create table driver_case (registration varchar2(60),
make varchar2(60),
model varchar2(60),
gvw number,
"year" number,
"body" varchar2(60));
and then try to do
select year, body from driver_case
then Oracle tries to find column "YEAR", "BODY" (remember, without an identifier without quotes gets converted to upper case) which is not the same as "year", "body" in your table.
Your solutions?
Don't mention column names under quotes in the DDL
If you disregard the above point, then you must mention the column names under quotes in all your DML statements.
I demonstrate the above point in this SQL Fiddle
Try this:
INSERT
WHEN city='palanpur' THEN INTO Ashish
When city='Aburoad' THEN INTO abhi
SELECT no, name
FROM arpit;