Skip to content
Advertisement

Generate One year dates using Oracle SQL

I have a table with three ID and Dates i want generate dates of one year from current date and before and after six months Then i want to plot three dates on this one year generated calandar I tried

SELECT (TO_DATE(SYSDATE)-180) + ROWNUM DT
  FROM (SELECT 1
          FROM DUAL
       CONNECT BY LEVEL <=180)

Advertisement

Answer

The concept of “month” is variable, so you may want to double check if “6 months” is really what you’re looking for. It can give unexpected results if you don’t know the oddities of date math.

That being said, a calendar which contains 6 months on either side of the current date can be generated with the following. Note that 184 days is the longest a 6 month span can last (July-December).

SELECT dt
  FROM (SELECT TRUNC(SYSDATE - 184) + LEVEL AS dt
          FROM dual
        CONNECT BY LEVEL <= 369)
 WHERE dt BETWEEN add_months(TRUNC(SYSDATE), -6) AND add_months(TRUNC(SYSDATE), 6);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement