Search code examples
sqloracle-databasetimerange

Oracle query - day / time range


Trying to figure out how to write a query where the criteria on the CONNECT_TIME column is only on FRIDAYS 9:00pm - SATURDAYS 1:00am. Everything else can be ignored.

Just not sure how to combine the days of the week and times......

Many thanks!


Solution

  •  WHERE CONNECT_TIME BETWEEN
           TRUNC(CONNECT_TIME, 'IW') + TO_DSINTERVAL('4 21:00:00')  -- FRIDAY 9PM
       AND TRUNC(CONNECT_TIME, 'IW') + TO_DSINTERVAL('5 01:00:00)   -- SAT 1AM
    

    Truncating a date to the start of the ISO week sets it to midnight Monday, and then we add the number of days and hours back in on each side of the BETWEEN operator.