Home >Database >Mysql Tutorial >How to Efficiently Extract Date Ranges in PostgreSQL?

How to Efficiently Extract Date Ranges in PostgreSQL?

Susan Sarandon
Susan SarandonOriginal
2024-12-29 14:38:10357browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn