[SQL] Generating a List of Dates Between Two Given Dates (From & To date) in Oracle SQL

 

When working with databases, there are situations where you need to generate a list of all dates within a specified date range. This can be useful in reporting, scheduling, or filling in missing dates in datasets. In this blog post, we'll explore how to achieve this in Oracle SQL using a recursive Common Table Expression (CTE).


Using Recursive CTE to Generate Dates in Oracle

Oracle allows us to use a recursive Common Table Expression (CTE) to generate a list of dates dynamically. Below is the SQL query that accomplishes this task:

WITH date_series (date_value) AS (
    SELECT TO_DATE('2024-03-01', 'YYYY-MM-DD') FROM DUAL
    UNION ALL
    SELECT date_value + 1 FROM date_series
    WHERE date_value + 1 <= TO_DATE('2024-03-10', 'YYYY-MM-DD')
)
SELECT date_value FROM date_series;


Explanation of the Query

  • The WITH date_series (date_value) clause creates a temporary table-like structure with a column named date_value.
  • The first SELECT statement initializes the date series with the starting date (2024-03-01).
  • The UNION ALL statement recursively adds one day to date_value.
  • The WHERE condition ensures that the recursion stops once the date_value reaches the specified end date (2024-03-10).
  • The SELECT date_value FROM date_series; retrieves all generated dates from the recursive CTE.
Note: if you don't use date_series (date_value) then you will get the below error:
ORA-32039: recursive WITH clause must have column alias list

Alternative Approach: Using CONNECT BY

Oracle also provides a simpler method using the CONNECT BY clause:

SELECT TO_DATE('2024-03-01', 'YYYY-MM-DD') + LEVEL - 1 AS date_value
FROM DUAL
CONNECT BY LEVEL <= TO_DATE('2024-03-10', 'YYYY-MM-DD') 
- TO_DATE('2024-03-01', 'YYYY-MM-DD')
+ 1;


Why Use CONNECT BY?

  • This approach is more efficient and does not require recursion.
  • It leverages Oracle's LEVEL pseudo-column to generate a sequence of numbers.
  • The formula TO_DATE('2024-03-10', 'YYYY-MM-DD') - TO_DATE('2024-03-01', 'YYYY-MM-DD') + 1 calculates the total number of days.

Comments

All Categories

Call Fusion BIP Report2 Change Password1 Code Combinations2 Compute Instance2 CTE1 Custom Images1 Customer1 Data Aggregation2 Database5 Date Conversion1 DB Adapter2 Decryption1 Development1 EBS4 Encryption1 ESS Jobs3 Examine1 FBDI3 Fusion APIs1 Fusion BIP7 GIT2 GL3 GL Journals1 GL_DAILY_CONVERSION_TYPES1 GL_DAILY_RATES1 ICS1 Identity Domain1 Integrations1 Java1 Journal Import1 Keys1 Legal Entity1 LookupTypeLOV1 LOV1 LOVs1 MultiPartAPIs1 Networking1 NVL2 NVL in OIC2 OCI13 OCI Billing1 OCI Compute6 OCI Cost Management1 OCI Events Service1 OCI Free Tier3 oci networking1 OCI Notifification Service1 OCI Security3 OIC4 OIC Mapper2 Oracle26 Oracle ADF17 Oracle APEX1 Oracle Apps59 Oracle Apps R126 Oracle ATP1 Oracle BIP8 Oracle Cloud13 Oracle Cloud Free Tier1 Oracle cloud Infrastructure10 Oracle Cloud Security2 Oracle Cloud VM1 Oracle DB4 oracle ebs5 Oracle ERP4 Oracle ERP Adapter2 Oracle ERP Cloud7 Oracle financials2 Oracle Forms1 Oracle Fusion57 Oracle Fusion BIP4 Oracle Fusion ERP17 Oracle Fusion Financials18 Oracle Integration Cloud3 Oracle OAF17 Oracle OCI14 Oracle OIC22 Oracle SOA 12c10 Oracle SQL17 Oracle VBCS1 Oracle VBS2 Oracle Visual Builder Cloud Service1 Oracle Visual Builder Studio2 Oracle Workflow Notifications1 OracleCloudTutorial1 Others10 Payables2 Payables Import1 Properties1 R121 Register BIP as ESS Job1 Reset Password1 Responsibility1 REST4 Security List1 Site Map1 SOAP2 SOAP API2 SOAP UI3 SQL16 SQL Functions3 SQL Queries14 SQL Query8 SQL Tips3 SSH1 TCA1 Value Sets1 VBCS1 vcn1 Virtual Machine2 Virtual Machines1 XML1 XSLT1
Show more