ORA-00905: missing keyword

EvanMPW picture EvanMPW · Jul 27, 2012 · Viewed 9.7k times · Source

I am getting ORA-00905: missing keyword error when trying to run the following query in Oracle:

select distinct MONTH_ID
from    DSS_MAIN.DIM_TIME a1
where   Case When TO_CHAR(SYSDATE,'MM') < 3
Then TO_CHAR(a1.MONTH_START_DATE,'YYYYMM') Between (TO_CHAR(SYSDATE,'YYYY')-1)||'03' And TO_CHAR(SYSDATE,'YYYYMM')
Else TO_CHAR(a1.MONTH_START_DATE,'YYYYMM') Between TO_CHAR(SYSDATE,'YYYY')||'03' And TO_CHAR(SYSDATE,'YYYYMM')
End;

The individual Between clauses work fine, so I am assuming there is something wrong with the Case syntax.

Any ideas?

Answer

hmmftg picture hmmftg · Jul 27, 2012

Try this:

SELECT DISTINCT MONTH_ID
  FROM DSS_MAIN.DIM_TIME A1
 WHERE TO_CHAR(A1.MONTH_START_DATE, 'YYYYMM') 
            BETWEEN CASE
                        WHEN TO_CHAR(SYSDATE, 'MM') < '03' THEN
                            (TO_NUMBER(TO_CHAR(SYSDATE, 'YYYY')) - 1) || '03'
                        ELSE
                            TO_CHAR(SYSDATE, 'YYYY') || '03'
                    END
                AND  TO_CHAR(SYSDATE, 'YYYYMM')

Case returns a value for further use, it will not be useful in the way you used it.