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

How to Efficiently Extract Date Ranges in PostgreSQL?

Susan Sarandon
Release: 2024-12-29 14:38:10
Original
366 people have browsed it

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
Copy after login

Alternatively, the query can be simplified using casting:

select i::date from generate_series('2012-06-29',
'2012-07-03', '1 day'::interval) i
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template