To dynamically retrieve dates in a date range using SQL, you can do this (example in mysql):
Create a table to store numbers from 0 to 9.
CREATE TABLE ints ( i tinyint(4) ); insert into ints (i) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
Run the query as follows:
select ((curdate() - interval 2 year) + interval (ti * 100 + ui * 10 + vi) day) AS Date from ints t join ints u join ints v having Date between '2015-01-01' and '2015-05-01' order by ti, ui, vi
This will create all dates between January 1, 2015 and May 1, 2015.
Output 2015-01-01 2015-01-02 2015-01-03 2015-01-04 2015-01-05 2015-01-06 ... 2015-05-01
The query joins the ints table 3 times and gets an extra number (from 0 to 999). He then adds this number as a daily interval, starting from a specific date, in this case, the date is 2 years ago. Any date range from 2 years ago and 1000 days in advance can be obtained using the above example. To generate a query that generates dates for more than 1000 days, simply join the ints table to allow up to 10,000 days of the range, etc.
anagnostatos
source share