PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Obter datas de um dia da semana em um intervalo de datas

create function f(dr daterange)
returns setof date as $$
    select d::date
    from generate_series(
        lower(dr), upper(dr), interval '1 day'
    ) s (d)
    where
        extract(dow from d) = 1 and
        d::date <@ dr;
    ;
$$ language sql;

select f(daterange('2014-01-01', '2014-01-20'));
     f      
------------
 2014-01-06
 2014-01-13