Click here to Skip to main content
15,881,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am working with SQL oracle and need help with coming up with a query to get the most recent date range of records for the month July. So if I ran the query today it will give me 07/01/2019 - 07/31/2019 range. If I ran the same query in September 2020 it will give me 07/01/2020 - 07/31/2020. Appreciate any help if possible.

What I have tried:

I have been looking at the add_months function but then that would require me to keep alternating the code to get the result which I am trying to avoid.
Posted
Updated 25-Feb-20 5:35am
v4
Comments
phil.o 25-Feb-20 11:18am    
What do you mean keep alternating the code? Please show your sql.
MadMyche 25-Feb-20 11:22am    
Please show some sample data as well as the code, or at least the table structure

1 solution

Schematically:
SQL
SELECT *
FROM TheTable
WHERE TheDate BETWEEN TO_DATE(CONCAT(TO_CHAR(sysdate, 'YYYY'), '-JUL-01'), 'YYYY-MON-DD') -- from YEAR-JUL-01 00:00:00
                  AND TO_DATE(CONCAT(TO_CHAR(sysdate, 'YYYY'), '-JUL-31'), 'YYYY-MON-DD') + 1 - (1 / (24 * 60 * 60)) -- to YEAR-JUL-31 23:59:59
Built from sql - oracle date range - Stack Overflow[^]
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900