How to do it in a PL SQL select query?
Main Topics
Browse All TopicsI need to generate a list of dates in between two date ranges with an increment of 15 days in a select query. for e.g if start date = 01-jan-2009 and end date is = 31-mar-2009
I want the result like the following:
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
actually you can generate your list with just sql...
however your dates aren't incrementing by 15 days every time
01-jan-2009 15-jan-2009 inc=14 days
16-jan-2009 31-jan-2009 inc=15 days
01-feb-2009 15-feb-2009 inc=14 days
16-feb-2009 28-feb-2009 inc=12 days
01-mar-2009 15-mar-2009 inc=14 days
16-mar-2009 31-mar-2009 inc=15 days
looks like you are "really" looking for 1st of month-15th of month and 16th of month to end of month for all months in your range.
In reply tp sdstuber, i tried your query and it works fine but the dates need not necessarily be first-mid-end of month dates, it can be any range, but if the date range is more than 15 days then i will break the range into 15 days period.
So if the date range is 1-jan-2009 to 20-jan-2009
then result should be in the following:
Also I have oracle 10g.
since you're going with consistent 15 day gaps, your original lists must not have been correct... that makes the query much easier
SELECT TO_DATE('05-jan-2009', 'dd-mon-yyyy') + ((LEVEL - 1) * 16),
LEAST(TO_DATE('05-jan-2009
TO_DATE('18-mar-2009', 'dd-mon-yyyy'))
FROM DUAL
CONNECT BY TO_DATE('05-jan-2009', 'dd-mon-yyyy') + ((LEVEL - 1) * 16) <=
TO_DATE('18-mar-2009', 'dd-mon-yyyy')
Business Accounts
Answer for Membership
by: schwertnerPosted on 2009-04-10 at 05:41:44ID: 24115092
it is a typical loop problem.
Oracle SQL doesn't support loops.
Use PL/SQL instead.