甲骨文:上个月的上个星期五

时间:2018-05-19 21:52:09

标签: oracle

    SELECT 
      TO_CHAR(NEXT_DAY(LAST_DAY(SYSDATE)-7, 'FRIDAY'),
                       ' DD MONTH, YYYY') "LAST_FRIDAY"
    FROM DUAL;

这将在上周五给我当月。我上个星期五需要一个月。

2 个答案:

答案 0 :(得分:2)

您将要关闭,只需要使用ADD_MONTH,如下所示:

1。与今天比较。

  SELECT 
      TO_CHAR(NEXT_DAY(LAST_DAY(add_months(SYSDATE,-1))-7, 'FRIDAY'),
                       ' DD MONTH, YYYY') "LAST_FRIDAY"
    FROM DUAL;

2。最后一天静止

select dt
from(
SELECT to_char(last_day(add_months(sysdate,-1)),'DD MONTH, YYYY') dt,
        to_char(last_day(add_months(sysdate,-1))-level,'DAY') dy
  FROM DUAL
  connect by rownum <= 7
) where trim(dy) = 'FRIDAY'

答案 1 :(得分:1)

您可以执行以下查询。

我的逻辑是:

1 - 获取上个月的最后一天

2 - 从第1项的日期开始获取星期几

3 - &#34; case&#34;得到愿望日期

select case
         when WLD = 7 then LD-1 -- if Saturday, get previous day
         when WLD = 6 then LD   -- if Friday, then ok
         when WLD = 5 then LD-6 -- if Thursday, get 6 days ago
         when WLD = 4 then LD-5 -- if Wednesday, get 5 days ago
         when WLD = 3 then LD-4 -- if Tuesday, get 4 days ago
         when WLD = 2 then LD-3 -- if Monday, get 3 days ago
         when WLD = 1 then LD-2 -- if Sunday, get 2 days ago
       end as LAST_FRIDAY_LAST_MONTH -- HERE IS THE LAST FRIDAY OF MONTH
  from (          
        select last_day(add_months(trunc(sysdate),-1))               as LD   -- last day of previous month
             , to_char(last_day(add_months(trunc(sysdate),-1)),'D')  as WLD  -- week day of last day of previous month
          from dual
       )