如何计算Postgresql中不包括星期六和星期日的2个日期之间的天数

时间:2017-04-11 08:31:32

标签: postgresql

如果:

return redirect()->away('https://www.your_url_here.com');

它返回这些日期之间的天数。我需要相同但不包括所有星期六和星期日。怎么样?

2 个答案:

答案 0 :(得分:2)

您可以使用dow排除它们,例如:

t=# with i as (
  select '2017-03-31'::DATE d1, '2017-03-01'::DATE d2
)
select count(1)
from i
join generate_series(d2,d1,'1 day'::interval) g on true
where extract(dow from g) not in (6,0);
 count
-------
    23
(1 row)

t=# with i as (
  select '2017-04-30'::DATE d1, '2017-04-01'::DATE d2
)
select count(1)
from i
join generate_series(d2,d1,'1 day'::interval) g on true
where extract(dow from g) not in (6,0);
 count
-------
    20
(1 row)

答案 1 :(得分:1)

如果我理解正确,你需要这个:

select count(*) from (
    select EXTRACT(DOW FROM s.d::date) as dd from generate_series('2017-03-01'::DATE, '2017-03-31'::DATE , '1 day') AS s(d)
) t
where  dd not in(0,6)