Skip to content
Advertisement

How to exclude between a particular date range each year in SQL

I’m trying to exclude the Christmas period between two dates every year in my database. This is the query I’m trying to run. From my understanding this should include every row, for every year with the exception of rows where the month is <= 12 and day <= 15 and where the month is <= 12 and day >= 26.

The result, however, doesn’t appear to return any rows at all!

  SELECT
      *
  FROM
      transactons
  WHERE
   event_date BETWEEN TO_DATE("2016-12-01")
                  AND TO_DATE("2021-12-31")
   
   AND ((EXTRACT(MONTH FROM event_date) <= 12 AND EXTRACT(DAY FROM event_date) <= 15)
   AND (EXTRACT(MONTH FROM event_date) <= 12 AND EXTRACT(DAY FROM event_date) >= 26))

Does anyone have any suggestions as to how to filter out Dec-15 to Dec-26 each year here?

Advertisement

Answer

You should specify your DBMS, because the following code can change. Please provide it and I will glade to update my answer.

For something quick and if I understand your logic:

  SELECT
      *
  FROM
      transactons
  WHERE
   (
    MONTH(event_date) = 12
    AND
    DAY(event_date) BETWEEN 12 AND 26
    )
   

You can reverse the query if you put NOT after the WHERE

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