How to Generate a range of dates using "CONNECT BY" in Oracle?

The CONNECT BY clause specifies the link between parent rows and kid rows of the hierarchy. The connect_by_condition is any condition, however, it should use the previous operator to consult with the parent row. Restriction on the CONNECT BY clause: The connect_by_condition cannot contain a daily subquery or a scalar subquery expression.

Here you can generate a list of END OF MONTH by Script not needing to create the table.

bellow is a sample script and result:

SELECT END_DATE_OF_MONTH
   ,'MONTH_'|| RANK() OVER(ORDER BY TO_CHAR(TO_DATE,'MM') - TO_CHAR(END_DATE_OF_MONTH,'MM') DESC) AS MON
   ,TO_DATE - END_DATE_OF_MONTH AS DAY
FROM (
SELECT DISTINCT LAST_DAY(TO_DATE('18-JUL-2022') + LEVEL-1) AS END_DATE_OF_MONTH
,TO_DATE('18-JUL-2022') AS FROM_DATE
,LAST_DAY(&PM_TDATE) TO_DATE
FROM DUAL
CONNECT BY LEVEL <=LAST_DAY(&PM_TDATE) - TO_DATE('18-JUL-2022') + 1
)D ORDER BY END_DATE_OF_MONTH;










Post a Comment

Previous Post Next Post