获取特定日期的 Oracle 查询

Oracle query for getting particular Date

我有一个 oracle 查询,return 是下周一的第一个星期日。 现在我有一个条件来检查已经过去的日期是否是当月的第一个星期日,那么我们需要当月的第二个星期日。 否则,下个月第一个星期日。

我的查询:

DEF  startDate = to_date('somedate', 'dd/mm/yyyy');
Select next_day(last_day(&startDate),'Sunday' )  from dual ; 

预期输出: 如果我们输入 2018 年 7 月 1 日,它必须 return 2018 年 7 月 8 日(第二个星期日)否则,除了第一个星期日之外的任何其他日子,例如(2018 年 7 月 2 日),它必须 return 2018 年 8 月 5 日.

Input      Expected Output
01-Jul-18   08-Jul-18,
02-Jul-18   05-Aug-18,
05-Aug-18   12-Aug-18,
19-Aug-18   02-Sep-18.

根据你的问题和评论中的描述,你想要这样的东西:

case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday') -- date is on first sunday
     then next_day(start_date, 'Sunday') -- next Sunday, which is second in month
     else next_day(last_day(start_date), 'Sunday') -- first Sunday of next month
end

在 CTE 中有一些示例日期,包括一些已讨论的以及其他的:

with cte (start_date) as (
            select date '2018-05-30' from dual
  union all select date '2018-06-01' from dual
  union all select date '2018-06-02' from dual
  union all select date '2018-06-03' from dual
  union all select date '2018-06-04' from dual
  union all select date '2018-06-30' from dual
  union all select date '2018-07-01' from dual
  union all select date '2018-07-02' from dual
  union all select date '2018-07-03' from dual
  union all select date '2018-07-04' from dual
  union all select date '2018-07-05' from dual
  union all select date '2018-07-06' from dual
  union all select date '2018-07-07' from dual
  union all select date '2018-07-08' from dual
  union all select date '2018-07-31' from dual
  union all select date '2018-08-01' from dual
  union all select date '2018-08-02' from dual
  union all select date '2018-08-03' from dual
  union all select date '2018-08-04' from dual
  union all select date '2018-08-05' from dual
  union all select date '2018-08-06' from dual
)
select start_date,
  to_char(start_date, 'Dy') as day,
  case when to_char(start_date, 'Dy') = 'Sun'
       then 'Yes' else 'No' end as is_sunday,
  case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday')
       then 'Yes' else 'No' end as is_first_sunday,
  next_day(trunc(start_date, 'MM') - 1, 'Sunday') as first_sun_this_month,
  next_day(trunc(start_date, 'MM') + 6, 'Sunday') as second_sun_this_month,
  next_day(last_day(start_date), 'Sunday') as first_sun_next_month,
  case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday') -- date is on first sunday
       then next_day(start_date, 'Sunday') -- next Sunday, which is second in month
       else next_day(last_day(start_date), 'Sunday') -- first Sunday of next month
  end as result
from cte;

得到

START_DATE DAY          IS_ IS_ FIRST_SUN_ SECOND_SUN FIRST_SUN_ RESULT    
---------- ------------ --- --- ---------- ---------- ---------- ----------
2018-05-30 Wed          No  No  2018-05-06 2018-05-13 2018-06-03 2018-06-03
2018-06-01 Fri          No  No  2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-02 Sat          No  No  2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-03 Sun          Yes Yes 2018-06-03 2018-06-10 2018-07-01 2018-06-10
2018-06-04 Mon          No  No  2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-30 Sat          No  No  2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-07-01 Sun          Yes Yes 2018-07-01 2018-07-08 2018-08-05 2018-07-08
2018-07-02 Mon          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-03 Tue          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-04 Wed          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-05 Thu          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-06 Fri          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-07 Sat          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-08 Sun          Yes No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-31 Tue          No  No  2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-08-01 Wed          No  No  2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-02 Thu          No  No  2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-03 Fri          No  No  2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-04 Sat          No  No  2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-05 Sun          Yes Yes 2018-08-05 2018-08-12 2018-09-02 2018-08-12
2018-08-06 Mon          No  No  2018-08-05 2018-08-12 2018-09-02 2018-09-02

result 栏是您感兴趣的栏目,其他栏目只是稍微展示一下工作原理。

与输入日期相比,结果日期乱序看起来很奇怪 - 2018-06-03 到 2018--06-10,然后它之前和之后的日期都到 2018-07-01。但这似乎就是你想要的。