How to iterate over a date range in PL/SQL

前端 未结 7 1861
梦毁少年i
梦毁少年i 2020-12-29 12:56

I need to write a report that generates summary totals against a table with date ranges for each record.

table data:
option   start_date   end_date
opt1              


        
7条回答
  •  一整个雨季
    2020-12-29 13:30

    One solution that I use for this is to convert the date range into an integer range that you can use in a for loop, then convert back to a date to do stuff with it. You can't do any joins or anything this way, but it's a much smaller solution that those already posted:

    declare
      start_date number;
      end_date number;
      business_date varchar2(8);
    begin
      start_date := to_number(to_char(to_date('2013-04-25', 'yyyy-MM-dd'), 'j'));
      end_date := to_number(to_char(to_date('2013-05-31', 'yyyy-MM-dd'), 'j'));
      for cur_r in start_date..end_date loop
        business_date := to_char(to_date(cur_r, 'j'), 'yyyy-MM-dd');
        dbms_output.put_line(business_date);
      end loop;
    end;
    

提交回复
热议问题