Skip to content
Advertisement

SQL Query to display dates weekly from current date

I have a requirement to display dates every week starting from current date for 48 months.

I was wondering if it is possible through SQL or i will have to write a function to achieve it.

Below is my SQL so far :-

SELECT
  CALENDAR_DATE
FROM
  CALENDAR --My Table Name
WHERE
  (
   CALENDAR_DATE  >=  trunc(sysdate)
   AND
   CALENDAR_DATE  <=  ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), 48)-1
  )

This would give me output as

9/10/2020
9/11/2020
9/12/2020
9/13/2020

Expected Output :-

 9/10/2020
   9/17/2020
   9/24/2020
   10/01/2020

Advertisement

Answer

If I understand correctly:

WHERE CALENDAR_DATE  >=  trunc(sysdate) AND
      CALENDAR_DATE  <=  ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), 48) - 1 AND
      MOD(CALENDAR_DATE - TRUNC(sysdate), 7) = 0

This uses the mod() operation on the date difference. You can also check the day of the week:

WHERE CALENDAR_DATE  >=  trunc(sysdate) AND
      CALENDAR_DATE  <=  ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), 48) - 1 AND
      TO_CHAR(CALENDAR_DATE, 'DY') = TO_CHAR(sysdate, 'DY')
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement