Skip to content
Advertisement

CASE statement in WHERE CALUSE – Missing keyword error

I have list of products which gets renewed on monthly or yearly or both. If the renewal value is ‘M’ then the product is renewed on monthly basis. If the renewal value is ‘Y’ the the product is renewed on yearly basis.

So my query is, If I choose method(M/Y) and a particular date(P_date), I want the details of products that are eligible for renewal.

For eg:

If I choose P_date as 06-01-2022 and renew as M then P_no 1001 should be selected

If I choose P_date as 06-01-2022 and renew as A then P_no 1002 should be selected

I wrote the below query

and got error:

Missing Keyword

What am I doing wrong?

Advertisement

Answer

You are trying to change the conditions that applied to the query based on the case expression, which won’t work; you could have that evaluate to say 0/1 and compare with than, but it’s just going to make it harder to read.

Instead, just use Boolean logic for the whole thing; assuming renew can only ever be M or Y then maybe:

or perhaps:

Although either way your conversion of p_date looks very odd. If that is a date data type (as it should be, if possible) then just do:

and if it’s a string convert it to a date, but not back to a string again:

Of course, that assumes start_date and end_date are also dates. If they are strings (and they shouldn’t be) then P_date wouldn’t need to be converted, but the comparison wouldn’t work as you expect with the format you’re seeing/using.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement