Invalid number error in where clause - sql

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.

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

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.

Minus operator returns error ora-01722

I have row, which is a character field, but is compared to numeric values in conditions. This causes ORA-01722 invalid number error under some circumstances.
Instead of: row in (1, 2, 3, 4, 5, 7)
It should be: row in (‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘7’)
This query returns ORA-01722:
(select * from table#production)
minus
(select * from table)
How can the query above be modified?

Oracle Error when dividing by Zero

I have a query where I need to get a certain number divided by 2 fields in 2 columns.
shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) as Shelve_DOC
I get the notorious error.
ORA-01476: divisor is equal to zero
01476. 00000 - "divisor is equal to zero"
*Cause:
*Action:
I have read around I need a CASE/IF but I'm not sure how to..
Any help would be appreciated.
SELECT
CASE WHEN (GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) = 0 THEN null
ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
END Shelve_DOC
FROM ...
WHERE ....
Should do the trick.
Something like this?
CASE WHEN NVL(y.FORECAST_QUANTITY,0) <= 0 AND NVL(z.sales,0) <= 0
THEN NULL
ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
END AS Shelve_DOC