简体   繁体   中英

Oracle SQL Invalid Number Error with Strings

I have a query like this...

SELECT 1,2,3 
FROM (SELECT CASE WHEN something THEN TO_CHAR(1)... END,2,3 
      FROM tables WHERE condtions) 
      WHERE 1 NOT LIKE 'String'

All columns are varchar2, I get a Invalid Number error with this query, pointing to the inner case statement. The inner query runs fine seperately.. the entire query runs fine if I remove the outer WHERE condtion.

I understand this has to do something with the SQL Optimizer, but what am I doing wrong here and how can I get around this? Using To_CHAR for the String or outer select statement aren't working...


Updating for Better understanding,

SELECT COL1, COL2, COL3 FROM (
SELECT CASE WHEN LOGIC THEN TO_CHAR(1) ELSE TO_CHAR(0) END AS COL1, 
COUNT(SOME_COL) AS  COL2 , COUNT(SOME_COL2) AS COL3 FROM TABLES WHERE CONDTIONS
) WHERE COL1 NOT LIKE ‘0’

I was told the Oracle SQL Optimizer is interfering and causing the above stated error and that the query has to be written in a different way.

Indicate that 1 is a column name by surrounding it with back ticks.

SELECT 1,2,3 FROM (SELECT CASE WHEN something THEN TO_CHAR(1)... END,2,3 
FROM tables
WHERE condtions) WHERE `1` NOT LIKE 'String'

You need to use proper column names (that start with a letter) - 1,2,3 won't work (unless quoted):

create table tab1 as 
select 1 as pk from dual
union all
select 2 as pk from dual
union all
select 3 as pk from dual;

SELECT col1,col2,col3 
FROM (SELECT (CASE WHEN 1=2 THEN TO_CHAR(1) else to_char(2) END) as col1 ,
      2 as col2,
      3 as col3
      FROM tab1 WHERE pk > 1) 
      WHERE col1 NOT LIKE '%1%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM