search

Home  >  Q&A  >  body text

Query an SQL table for dates that start/end within a range, and dates that fall within the range but start before/after the range

Overview:

I have a SQL database table where each row (event) contains a start and end date formatted as a timestamp.

Target:

Show events for a given week, these include events that start and end within that week, but may also take place within that week (their start/end dates may be within that week, or they may be before that week begins and ends after that week). Example of expected result:

I've tried using a BETWEEN query and a query using start > {$week_start} AND end < {$week_end}, but neither method takes into account "overlapping" dates.

Question: Is it possible to write a single query that covers all these cases?

BTW, if there is a better solution, I would accept using a date format other than timestamp.

P粉147747637P粉147747637523 days ago667

reply all(1)I'll reply

  • P粉222320176

    P粉2223201762023-07-22 00:35:31

    The logic you are looking for is:

    start <= {$week_end} AND end >= {$week_start}

    reply
    0
  • Cancelreply