Re: Calculating Days/Time(Are Loops Neccessary?) - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: Calculating Days/Time(Are Loops Neccessary?)
Date
Msg-id 202309201520.jqs4jeruhezh@alvherre.pgsql
Whole thread Raw
In response to Calculating Days/Time(Are Loops Neccessary?)  (Anthony Apollis <anthony.apollis@gmail.com>)
List pgsql-general
On 2023-Sep-19, Anthony Apollis wrote:

> I have inherited this code, problem is it is over code, i believe. The
> package is gonna run once a month and this code run is a loop. How can this
> loop be running and checking data up until last day, if it only run once a
> month?

I didn't stop to understand your problem fully, but if you need the set
of calendar days in one month, you can use the generate_series()
function around some arithmetic that gives you the start of the month
plus when it ends, something like this:

with onedate(theday) as (values (date '2023-02-03'))
select g::date
from onedate,
     generate_series(date_trunc('month', onedate.theday),
        date_trunc('month', onedate.theday) + interval '1 month' - interval '1 day',
        '1 day') g ;

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"El sabio habla porque tiene algo que decir;
el tonto, porque tiene que decir algo" (Platon).



pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Calculating Days/Time(Are Loops Neccessary?)
Next
From: Erik Wienhold
Date:
Subject: Re: Changed functionality from 14.3 to 15.3