Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Case statements inside a Where clause

Don_WilsonOct 28 2015 — edited Oct 28 2015

Hi group,

I know this has been asked multiple times, but I simply don't grasp using a case statement inside a WHERE clause.  So I need some help:

My current WHERE clause reads:

a16.FULL_DATE between (SELECT (TRUNC(SysDate - 8)) From Dual) and (Select(TRUNC(SysDate - 2)) From Dual)

However I need to "automate" this a bit based on the current date/day of the month.  I need my case statement to say:

(Case      when        to_char(sysdate-3,'mm') <> to_char(sysdate,'mm')

    then    a16.FULL_DATE between (SELECT(trunc(ADD_MONTHS((LAST_DAY(SysDate-3)),-1)+1) and (SELECT(add_months(trunc(SysDate-2),-1)) from Dual)

    Else    a16.FULL_DATE between (SELECT trunc(ADD_MONTHS((LAST_DAY(SysDate)),-1)+1) From Dual) and (Select(TRUNC(SysDate - 2)) From Dual)

However I have an error on "Else".  Can someone explain what is wrong and how to fix this?  I've tested both the "Then" and "Else" syntax and both will run individually.  So I'm pretty sure it doesn't like my CASE statement in general.

In advance, thanks for your help.

Don

This post has been answered by SomeoneElse on Oct 28 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 25 2015
Added on Oct 28 2015
10 comments
3,840 views