Home >Database >Mysql Tutorial >How to Efficiently Extract Date Ranges in PostgreSQL?
Date Range Extraction in PostgreSQL
In PostgreSQL, extracting a list of dates within a specified range is a common task. This article discusses the most effective methods for this purpose.
Generating Sequential Dates
One approach involves the combination of the generate_series() function with date manipulation operators. For instance, the following query retrieves dates between '2012-06-29' and '2012-07-03':
select CURRENT_DATE + i from generate_series(date '2012-06-29'- CURRENT_DATE, date '2012-07-03' - CURRENT_DATE ) i
Alternatively, the query can be simplified using casting:
select i::date from generate_series('2012-06-29', '2012-07-03', '1 day'::interval) i
The above is the detailed content of How to Efficiently Extract Date Ranges in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!