Oracle gem of the day: variable date range in WHERE clause

jdavidb on 2006-06-09T19:10:06

This was developed in response to a request for help from a former boss. The problem is: I want to select all records from a table more recent than a certain date. In most cases, I want all records from the most recent 24 hours. However, on Mondays, I want all records from the most recent 72 hours. I know this means I need a conditional in the WHERE clause somewhere, but what is it, and where does it go?

Answer (and this was fun to figure out together):

SELECT *
FROM datetable
WHERE thedate < CASE WHEN TO_CHAR(SYSDATE, 'DY') = 'MON' THEN SYSDATE - 3
                     ELSE SYSDATE - 1 END
/