【问题标题】:How to find the last 'working' day of last month?如何找到上个月的最后一个“工作日”?
【发布时间】:2018-06-27 17:30:07
【问题描述】:

有没有办法找到上个月的最后一个工作日?我知道我可以使用SELECT (date_trunc('month', now())::date - 1) 获得上个月的最后一天,但我如何获得最后一个“工作日”?

【问题讨论】:

标签: sql postgresql date


【解决方案1】:

我相信这可以满足您的需求:

select (date_trunc('month', current_date) + interval '1 month' -
        (case extract(dow from date_trunc('month', current_date) + interval '1 month')
              when 0 then 2
              when 1 then 3
              else 1
         end) * interval '1 day'
       ) as last_weekday_in_month

此类请求通常表明需要日历表。

【讨论】:

  • 不错。但是,这将返回当月的最后一周。 OP 想要 最后一个月的最后一个工作日。
【解决方案2】:

背负戈登的回答,我认为这就是你想要的:

SELECT (
    date_trunc('month', current_date) - interval '1 day' -
    (case extract(dow from date_trunc('month', current_date) - interval '1 day')
         when 0 then 2
         when 6 then 1
         else 0
      end) * interval '1 day'
)::date as last_weekday_in_last_month;

假设您的周末是 0(星期日)和 6(星期六)。它使用 OP 的原始逻辑查找上个月的最后一个日期,然后使用 Gordon 的 CASE 逻辑在最后一个日期为 0 或 6 时减去更多天数。

【讨论】:

  • 你太棒了!谢谢!!
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 2023-02-15
  • 2016-09-23
  • 2016-06-17
  • 1970-01-01
  • 2013-04-08
  • 2010-12-13
  • 2010-12-02
相关资源
最近更新 更多