Invalid number error while executing query - sql

I need your help in an error which I'm getting, I'm using the below query in order to see the average of a percentage column in my table, but it is giving me the below error message:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
Query:
SELECT 'Skybox' as Platform,avg(PER_OF_VIOLATING_RULES) as
ComplianceCalculation from table_name
Values in the PER_OF_VIOLATING_RULES column:
PER_OF_VIOLATING_RULES
32.08%
55.77%
54.19%
54.84%
16.13%
23.22%
29.50%
5.56%
48.50%
56.04%
PER_OF_VIOLATING_RULES column is a varchar2 Datatype.

Try to replace the % sign:
SELECT 'Skybox' as Platform,avg(replace(PER_OF_VIOLATING_RULES, '%', '')) as ComplianceCalculation from table_name

Related

Format Phone number in SQL using SUBSTR command but keep getting error

Trying to list all instructors. Show salutation in all lower cases, last name in all upper cases and phone number in the format of '(xxx) xxx-xxxx'
I need to use the SUBSTR command but I keep getting an error.
Here is the query:
SELECT LOWER(salutation)
, UPPER(last_name)
,'(' + SUBSTRING(PhoneNumber, 1, 3) + ')' ,SUBSTRING(PhoneNumber, 4, 3) + '-' +SUBSTRING(PhoneNumber, 7, 4)
FROM instructor;
Error message:
ORA-00904: "SUBSTRING": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 6 Column: 122
EDIT #1 -
SELECT LOWER(salutation)
, UPPER(last_name)
, '(' + SUBSTRING||PhoneNumber, 1, 3|| + ')'
, SUBSTRING||PhoneNumber, 4, 3|| + '-' +SUBSTRING||PhoneNumber, 7, 4||
FROM instructor;
Error message:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 7 Column: 1
Am I not putting the || in the right places?
EDIT #2 -
SELECT LOWER(salutation)
, UPPER(last_name)
, '(' ||SUBSTR(phone'(XXX)XXX-XXXX', 1, 13) || AS Phone_Number
FROM instructor;
Error message:
ORA-00907: missing right parenthesis
I just need to get the phone number from the table and format it in (xxx)xxx-xxx. I can run the following query:
SELECT LOWER(salutation)
, UPPER(last_name)
, SUBSTR('(XXX)XXX-XXXX', 1, 13) AS Phone_Number
FROM instructor;
But, I get the following response: screenshot. It needs to show the actual phone number.
Thank you or your time and patience!

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.

missing parenthesis error

So I created the following code:
Select c_id, c_name
From bk_CustWithOrders
where to_char(order_date,'MM-YYYY')=prevMonth(sysdate '2014-11-27', 4)
INTERSECT
Select c_id, c_name
From bk_CustWithOrders
where to_char(order_date,'MM-YYYY')=prevMonth(sysdate '2014-11-27', 3)
INTERSECT
Select c_id, c_name
From bk_CustWithOrders
where to_char(order_date,'MM-YYYY')=prevMonth(sysdate '2014-11-27', 2);
and I get the following error message:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 3 Column: 55
I've tried adding parenthesis but then I incur another error. Not sure what to do.
I dont know anyting about the function prevMonth.
But, What I noticed is there is no separator between sysdate and '2014-11-27'.
prevMonth(sysdate '2014-11-27', 2);
Use ',' between sysdate and '2014-11-27' and recheck:
prevMonth(sysdate, '2014-11-27', 2);
else you have to use any one of sysdate or '2014-11-27'
prevMonth(sysdate, 2);
prevMonth('2014-11-27', 2);
Line 3, column 55 is exactly at start of this part:
'2014-11-27', 4)
He's saying : I don't understand why you put a whitespace as separator.
sysdate '2014-11-27' is invalid.
If you only want to specify a date literal, it should be: date '2014-11-27'
sysdate is a function that returns the current date. If you just want to pass the current date to the function, then use only sysdate.

inconsistent datatypes: expected - got CHAR

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