案例陈述有些麻烦

时间:2019-09-15 17:53:49

标签: sql oracle plsql

我需要从2008年1月的每个一月的第一天开始,然后显示这一天,然后制作一个case语句,其中显示一串文本。

我尝试查找Oracle指南,但在此特定上下文中找不到任何内容

Declare
lv_date date;
day_of_week   VARCHAR2(30);
day_of_week_exp   VARCHAR2(30);
begin
lv_date := TO_DATE('1-Jan-2008','dd-mon-yyyy');
for i in 1..11 
loop
day_of_week := TO_CHAR(lv_date,'DAY');
DBMS_OUTPUT.PUT_LINE(day_of_week);

CASE
WHEN day_of_week = 'MONDAY' THEN DBMS_OUTPUT.PUT_LINE('moody' || 
day_of_week);
end case;

lv_date := ADD_MONTHS(lv_date,12);
end loop;

end;

2 个答案:

答案 0 :(得分:1)

您不需要PL / SQL,但可以使用SQL直接获得所需的结果:

select 2007 + level as year,
       trunc(to_date(2007 + level,'YYYY'),'YYYY') as first_day_of_year,
       to_char(trunc(to_date(2007 + level,'YYYY'),'YYYY'),'Day') as first_day_week
  from dual
 connect by level <= 2018 - 2008 + 1;

如果要按星期一进行过滤,则通过将上述SQL转换为游标来使用PL / SQL:

SQL> set serveroutput on
SQL> begin
  for c in 
    ( select 2007 + level as year,
             trunc(to_date(2007 + level,'YYYY'),'YYYY') as first_day_of_year,
             to_char(trunc(to_date(2007 + level,'YYYY'),'YYYY'),'Day') as day_of_week_exp
        from dual
     connect by level <= 2018 - 2008 + 1
    )
  loop
   if trim(c.day_of_week_exp) = 'Monday' then
    dbms_output.put_line('moody' ||c.day_of_week_exp);
   end if;
  end loop;
end;
/

Demo

答案 1 :(得分:0)

BDW,所需要做的就是将else部分添加到您的case语句中。

案例,当day_of_week ='MONDAY'然后DBMS_OUTPUT.PUT_LINE('moody'|| day_of_week); 否则为null; 最终案例