PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Aggiungere un numero di giorni lavorativi a una data

Genera una serie di giorni e numera con row_number(), escludendo weekend e festività:

create or replace function add_business_day(from_date date, num_days int) returns date as $fbd$ select d from ( select d::date, row_number() over (order by d) from generate_series(from_date+1, from_date+num_days*2+5, '1d') d where extract('dow' from d) not in (0,6) and d not in (select pyha from pyha) ) s where row_number = num_days $fbd$ language sql;

In alternativa, puoi usare un ciclo PL/pgSQL.