Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DATE functions - Weekly report

ziggy25Jun 5 2007 — edited Jun 6 2007
I have a batch job that runs everyday. I've been tasked to modify the batch job to produce a report every Monday morning.
The report should produce a summary of data for the previous week. i.e. the week before the Monday (Monday to Sunday).

I tried to do this and would like some feedback from you experts. I have a feeling there is a better/easier way to do this. So please if you can suggest a more easier way then please let me know.

Here is what i've done..

- Everyday the batch job runs it will check whether the current day is a Sunday. If it is then it will produce the report
- The end date will be the current sysdate i.e. Sunday being the end of the week.
- The start date is calculated by subtracting 6 from the end date. (To get the first day of the week - i.e. Monday)
- Subtracting 6 will be a problem if we are on the first week of any month because we might get the date for the previous month. The report should only produce the summary for the week in the current month so If we are in the first week of the month the start date will be the first day of the month regardless of what date the begining of the week is.

Being new to PL/SQL (and programming) im wondering whether you think this will work and will it work everyday every month and every year (including leap years).

Could you have a look and let me know if i should change anything or you think something might cause problems later in teh future.

Thanks in advance.
PROCEDURE weekly report
IS  
lv_start_date DATE;
lv_end_date DATE;
BEGIN
    IF To_Char(To_Date(SYSDATE),'DAY')='SUNDAY' THEN  --Check for end of week
                 
        -- Get last day of the week         
        lv_end_Date:=SYSDATE;        
        -- Get first day of the week (check to be within current month)
        
        IF TRUNC((TO_DATE(SYSDATE)-6),'MM')=TRUNC((TO_DATE(SYSDATE)),'MM') THEN
          -- not gone to previous month
          lv_start_date:=TO_DATE(SYSDATE)-6);
        ELSE
          -- gone to previous month so just use first day of the month
          lv_start_date:=TRUNC(SYSDATE,'MONTH');
        END IF;
        
      -- Run report
      stats_package.producestats(lv_start_date,lv_end_Date);
    ELSE
      -- Not ready to produce report
    END IF;
END weekly report;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2007
Added on Jun 5 2007
16 comments
911 views