Missing Keyword Error in Oracle Case When SQL Statement

ORA-00905: missing keyword in case statement

You can't have a boolean value as a selectable something in a query in oracle, you can only make boolean expressions in e.g. the WHERE/ON clauses etc

i.e. this is invalid:

select case when 1=1 then 2>3 else 4>5 end from dual
^^^
can't have something that evaluates to a boolean type here

This is valid:

select case when 1=1 then 'TRUE' else 'FALSE' end from dual

You could later compare these values to something to realize a boolean:

WHERE CASE WHEN x=y THEN 'T' ELSE 'F' END = 'T'

But you can't use booleans on their own.. This is also invalid:

WHERE CASE WHEN x=y THEN 1=1 ELSE 1=0 END

in your case, promote the booleans the case is trying to realize, into the WHERE predicates:

WHERE (
/*CASE
WHEN*/ TO_CHAR(SYSDATE,''MM'') <= ''06'' /*THEN*/ AND enrollement_dt <= to_date(''12''||(EXTRACT(YEAR FROM SYSDATE)-1), ''MMYYYY'')
) OR
/*ELSE*/ enrollement_dt >= to_date(''07''||(EXTRACT(YEAR FROM SYSDATE)), ''MMYYYY'')
/*END*/

(I left the case when in as comments to show you what was edited)

missing keyword error in oracle CASE WHEN sql statement

 CASE EVENT_ID WHEN WF.STATUS_ID=0 THEN EVENT_ID=10003 
WHEN WF.STATUS_ID=1 THEN EVENT_ID=10018 END AS EVENTID

You have mixed two different syntax of CASE statement.

1.simple_case_statement

   CASE [ expression ]

WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n

ELSE result

END

2.searched_case_statement

CASE 

WHEN expression condition_1 THEN result_1
WHEN expression condition_2 THEN result_2
...
WHEN expression condition_n THEN result_n

ELSE result

END

Change your expression to -

CASE 
WHEN WF.STATUS_ID=0 THEN 10003
WHEN WF.STATUS_ID=1 THEN 10018
END AS EVENTID

Follow this link to see the documentation for both the syntax.

Update OP says he still gets the missing keyword error. This is a test case to show it is not true. The missing keyword will be fixed with correct CASE statement.

SQL> CREATE OR REPLACE
2 PROCEDURE EXT_SELF_10003_SIGWF
3 AS
4 BEGIN
5 -- first empty TEMP_WF_WORKFLOW table
6 EXECUTE IMMEDIATE 'TRUNCATE TABLE TEMP_WF_WORKFLOW';
7 -- get WF_WORKFLOW table data
8 INSERT
9 INTO TEMP_WF_WORKFLOW
10 (
11 STATUS_ID,
12 EVENT_ID,
13 ORDER_NUMBER
14 )
15 SELECT WF.STATUS_ID,
16 CASE
17 WHEN WF.STATUS_ID=0
18 THEN 10003
19 WHEN WF.STATUS_ID=1
20 THEN 10018
21 END AS EVENTID,
22 TSM.ORDER_NUMBER
23 FROM WF_WORKFLOW@FONIC_RETAIL WF
24 JOIN TMP_SOAP_MONITORING_IDS TSM
25 ON TSM.SUBSCRIPTION_ID=WF.SUBSCRIPTION_ID
26 WHERE TSM.order_type ='SELF_REGISTRATION'
27 AND WF.NAME ='SIGNUP_MOBILE_PRE_PAID';
28 COMMIT;
29 END EXT_SELF_10003_SIGWF;
30 /

Warning: Procedure created with compilation errors.

SQL> sho err
Errors for PROCEDURE EXT_SELF_10003_SIGWF:

LINE/COL ERROR
-------- --------------------------------------------------
7/3 PL/SQL: SQL Statement ignored
23/8 PL/SQL: ORA-00942: table or view does not exist
SQL>

Oracle case missing keyword

First condition handle when both date are not null

Second one validate when one of the dates is null

select * 
from LARGE_RELATION LR
where
( STARTDATE IS NOT NULL
AND ENDDATE IS NOT NULL
AND LR.END_DT BETWEEN to_date(STARTDATE, 'yyyymmdd')
AND to_date(ENDDATE, 'yyyymmdd')
) OR
( (STARTDATE IS NULL OR ENDDATE IS NULL)
AND (LR.END_DT IS NULL OR LR.END_DT > SYSDATE - 7)
)

How to fix ORA-00905 missing keyword with case expression

There is a syntax error in the following two lines

sum(case when cv_amount > 0 then cv_amount else 0) end as d, 
sum(case when cv_amount < 0 then cv_amount*-1 else 0) end as c

the end should be with in the parentheses, so the query wil be:

sum(case when cv_amount > 0 then cv_amount else 0 end) as d, 
sum(case when cv_amount < 0 then cv_amount*-1 else 0 end) as c

SQL ORACLE ERROR: ORA-00905 Missing Keyword

The CASE syntax has 2 forms.

Don't mix them.

select n
, case n
when 1 then 'A'
when 2 then 'B'
else 'C'
end as form1
from (select 1 as n from dual) q

N | FORM1
-: | :----
1 | A
select n
, case
when n=1 then 'A'
when n>1 then 'B'
else 'C'
end as form2
from (select 2 as n from dual) q

N | FORM2
-: | :----
2 | B
select n
, case n
when n=1 then 'A'
when n>1 then 'B'
else 'C'
end as badmix
from (select 3 as n from dual) q

ORA-00905: missing keyword

db<>fiddle here

So this is wrong:

CASE p.PATIENT_ID when to_date('2022-01-07', 'YYYY-MM-DD') < p.LEAVING_DATE then 1 else null END

SQL Case statement throwing missing keyword error

You need a comparison operator outside the case statement:

Select * from users_t t
where
(case when sysdate <= to_date('20130131', 'yyyymmdd')
then 254664
else 1259753
End) = t.user_id

However, you can write this without the case statement:

select *
from users_t t
where ((sysdate <= to_date('20130131', 'yyyymmdd') and t.user_id = 254664) or
((sysdate > to_date('20130131', 'yyyymmdd') and t.user_id = 1259753)

ORA-00905: missing keyword for case statement

Your CASE statement has a couple of problems:

CASE var_salary -- you don't need var_salary here
WHEN salary <= 10000 THEN salary:= salary* .02 /*I am getting an error over here*/ -- you don't need "salary:=" here
WHEN salary BETWEEN 10000 AND 15000 THEN salary := salary * .02
WHEN salary BETWEEN 15000 AND 20000 THEN salary := salary * .025
END AS salary

So correcting for these issues it should read just:

CASE WHEN salary <= 10000 THEN salary * 0.2
WHEN salary BETWEEN 10000 AND 15000 THEN salary * 0.2
WHEN SALARY BETWEEN 15000 AND 20000 THEN salary * 0.25
END AS salary

You might also note that the cases for <= 10000 and BETWEEN 10000 AND 15000 are the same, and so could be combined.

Also keep in mind that BETWEEN ... AND is inclusive, so a value of 15000 will return 15000 * 0.2 (3000), rather than * 0.25.

Also, please note that I corrected your numbers ... 20% is 0.2, not .02.



Related Topics



Leave a reply



Submit