inconsistent datatypes: expected - got CHAR - sql

I have a senario where, I have the following Where Clause in my query. I am using Oracle 11g (11.2.0.1)
select * from aTable WHERE (attribute_name IN
(SELECT * FROM TABLE(CAST(:pbindvar AS CHARTABLETYPE)))or :pbindvar IS NULL)
The value for this bind variable is a array, but when I am trying to pass a null value to this bindvariable I am getting the following error
Expected - Got Char Error
Could anyone please let me know how am I supposed to pass a null value to this Bind variable so that the query will return me all the set of rows without applying the where condition.
Stack trace is as follows
ORA-00932: inconsistent datatypes: expected - got CHAR
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
I tried the following as well but with no luck still getting the same exception
I tried with the conditional where clause still getting the same error
WHERE (CASE WHEN :pbindvar is NULL THEN 1
WHEN attribute_name IN (SELECT * FROM TABLE(CAST(:pbindvar AS CHARTABLETYPE))) THEN 1
ELSE 0
END) = 1
If I change the second condition where clause as mentioned below, the query is returning with all rows
WHERE (CASE WHEN :pbindvar is NULL THEN 1
WHEN attribute_name IN (:pbindvar) THEN 1
ELSE 0
END) = 1
Sample Query using hr schema
select * from Departments where department_name in (Select * from table(cast(:pbindvar as CHARTABLETYPE))) or :pbindvar is Null;
create query for type
create or replace type chartabletype
as table of VARCHAR2(4000);
The issue occurs only when the null is passed to bind variable

Related

Oracle SQL export to csv show error: ORA-01722: invalid number

table_a table_b
-------------- ----------
product,amount,pcode rounding,pcode
-------------- ----------
apple,100.00,001 0.02,001
orange,150.02,001 -0.02,001
output
----------
apple,100.02
orange,150.00
select a.product||','||sum(a.amount)+b.rounding from table_a a,table_b b
where a.pcode=b.pcode
group by a.product,b.rounding
Hi , I trying to spooling this query to csv format, but showing error:
ORA-01722 invalid number.
when I remove +b.rounding then no issued at all, may I know how to use sum(a.amount)+b.rounding in my query ?
your kinds assist is much appreciated.
The error has nothing to do with "export to csv" or with aggregation. You can reproduce it very simply, like this:
select 'a' || 1 + 3 as result from dual;
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The reason is very simple: concatenation (the || operator) has the same precedence as addition (+). Since in your formula concatenation comes first, it is performed first. In my example, the number 1 is converted (implicitly) to the string '1' and concatenated to 'a', resulting in 'a1'. Then this must be added to 3. To do that, Oracle tries to convert 'a1' (implicitly) to number, and obviously that fails, with the same error you observed.
If you expected the result to be the string 'a4' (in my example), the solution is trivial: use parentheses.
select 'a' || (1 + 3) as result from dual;
RESULT
------
a4
The same applies to your situation.
Note that the following works - showing that || and + have the same precedence; concatenation is not stronger than addition, they are simply performed in order, left to right.
select 1 + 3 || 'a' as result from dual;
RESULT
------
4a
Here we don't need parentheses, because 1 + 3 = 4, and then that is converted to the string '4' and then 'a' is concatenated to it.

ORA-01722: invalid number - getting this error

I am trying to execute below in oracle sql developer
select code, case when (code = 'SS') then 1 else to_number(code) end as code_modified
from pxrptuser.WBS
But I am getting error.
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The output should be -
code code_modified
D0DV-IMS null
gWBS null
8 8
1 1
SS 1
Please help me with the actual query
You have strings in your data that cannot be converted to numbers (other than "SS").
Starting Oracle 12.2, you can use the on conversion error clause to to_number() to return null for invalid values:
select code,
case
when code = 'SS' then 1
else to_number(code default null on conversion error)
end as code_modified
from pxrptuser.WBS
In earlier versions, one alternative uses a regex. If your numbers have no decimal part, as showned in your data, it is simpler:
select code,
case
when code = 'SS' then 1
when not regexp_like(code, '\D') then to_number(code)
end as code_modified
from pxrptuser.WBS

Invalid number error in where clause

I am executing a query in Oracle database. The column and everything is correct but I am getting an Invalid Number error for below query:
select COUNT(*) AS "COUNT" from NE.STRUCT B
where B.STRUCT_TYPE in ('IDC')
and NET_ENTITY_ID is not null
and length(NET_ENTITY_ID) = 18
AND regexp_like(SUBSTR(NET_ENTITY_ID,15,1),'[^A-Z]')
and TO_NUMBER(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4)) < 6000;
NET_ENTITY_ID field has only one data ABCDEFGHXXXXNB0001.
But not necessary that it will always be having one data. This is just for resolving the issue I am considering only this.
Error Message:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The problem is that Oracle -- and any other database -- does not guarantee the order of evaluation of clauses in a WHERE. You can get around this using CASE:
where B.STRUCT_TYPE in ('IDC') and
NET_ENTITY_ID is not null and
length(NET_ENTITY_ID) = 18 AND
regexp_like(SUBSTR(NET_ENTITY_ID, 15, 1), '[^A-Z]') and
(CASE WHEN regexp_like(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4), '^[0-9]{4}$'
THEN TO_NUMBER(SUBSTR(NET_ENTITY_ID,(length(NET_ENTITY_ID) -3), 4))
END) < 6000;
You need to ensure that the last 4 chars are numeric before using TO_NUMBER on them.
This will do it:
select COUNT(*) AS "COUNT" from
( SELECT * FROM NE.STRUCT B
where B.STRUCT_TYPE in ('IDC')
and NET_ENTITY_ID is not null
and length(NET_ENTITY_ID) = 18
AND regexp_like(SUBSTR(NET_ENTITY_ID,15,1),'[^A-Z]')
AND regexp_like(SUBSTR(NET_ENTITY_ID,-4),'[0-9]')
)
where TO_NUMBER(SUBSTR(NET_ENTITY_ID,-4)) < 6000;
NB I simplified your SUBSTR for obtaining the last 4 characters.

Find all database records where the char data length in a column is larger than X

Database: Oracle 11g
Environment: Windows server. SQLPlus.
I am trying to query for all the records where the char data length in a column is larger than 10K. The column data type is LONG (which seems to be obsolete: http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1830).
Columns defined as LONG can store variable-length character data containing up to 2 gigabytes of information.
The following post is somewhat addressing this issue:
Select something that has more/less than x character
I have followed the instructions without luck.
I have tried a few variations of the ‘length’ function but still getting errors:
SQL> select * from tbl_name where LEN(notes) > 1;
select * from tbl_name where LEN(notes) > 1
*
ERROR at line 1:
ORA-00904: "LEN": invalid identifier
SQL> select * from tbl_name where length(notes) > 1;
select * from tbl_name where length(notes) > 1
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got LONG
Thanks in advance!
I suggest you create a function that calculate length of notes,receiving as parameter your table id (I asumme tbl_name_id) like this:
CREATE OR REPLACE function get_length(val long) return number
is
res long;
begin
select notes into res from tbl_name where val = tbl_name_id;
return length(res);
end;
And then you can do this:
select * from tbl_name where get_length(tbl_name_id) > 1
You can also see here http://www.techonthenet.com/oracle/questions/long_length.php

Select part of a string from a object type column SQL

So shortly I am receiving an SQL Error: ORA-00932: inconsistent datatypes: expected NUMBER got OE.CUST_ADDRESS_TYP
00932. 00000 - "inconsistent datatypes: expected %s got %s" when I am trying to retrieve a part of a string data.
SELECT dbms_lob.SUBSTR(cus.cust_address, 0, INSTR(cus.cust_address, ',')-1) AS output
FROM oe.customers cus;
So basically that's my statement.
The data looks like : OE.CUST_ADDRESS_TYP('322 E Michigan St','53202','Milwaukee','WI','US')
And I only want to display the city, which in this case would be Milwaukee.
However, the data_type of cust_address column is set to CUST_ADDRESS_TYP which is an object column of type address_typ.
I just want to select part of a string for instance, I want to only select Milwaukee from OE.CUST_ADDRESS_TYP('322 E Michigan St','53202','Milwaukee','WI','US').
CUST_ADDRESS CUST_ADDRESS_TYP Yes 4 Object column of type address_typ.
This is how it is showed in the table.
Really buggers me off as the statement works on varchar type columns but not on this one.
Kind regards,
P.S. Fixed it by using the object type column, didn't use Treat tho. Thanks anyaway.
See object type request semantic in Oracle documentation:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions198.htm
SQL> create type comp_type is object(
2 x varchar2(10)
3 , y varchar2(10)
4 )
5 /
SQL> create table t (col comp_type)
2 /
SQL> insert into t values(comp_type('A','B'))
2 /
SQL> commit
2 /
SQL> select treat(col as comp_type).x, treat(col as comp_type).y from t
2 /
TREAT(COLA TREAT(COLA
---------- ----------
A B