oracleoracle10gjobsintervalsdbms-job

Oracle dbms_job: job have to run only weekday 6am-8pm


i have an oracle 10g Database with different jobs. One of thoses jobs have to run every two hours. Therefore the "next_dat" and "interval" parameters looks like that.

   ,next_date => to_date('27.04.2011 10:18:00','dd/mm/yyyy hh24:mi:ss')
   ,interval  => 'to_date(to_char(sysdate + (2/24), ''DD.MM.YYYY HH24:MI''), ''DD.MM.YYYY HH24:MI'')'

The jobs don't need to run at night. So the question is: is it possible to design for example "interval" in that way, that the jobs only runs between 6am and 8pm (in hh24 06:00 until 20:00).

My first idea (and last choice) is an addition job which sets the main-job to "broken" between 8pm an 6 am. But i don't like the idea of an different job.

Thank you!


Solution

  • You could use a case statement in the interval to check for the time of day then skip:

    case
      when to_char(trunc(sysdate, 'HH24') + 2/24, 'HH24') between '06' and '20' then
        trunc(sysdate, 'HH24') + 2/24 
      else
        trunc(sysdate)+1+(6/24)
    end
    

    This can be expanded to include a check on weekday. (You need to be careful on checking for the day of week as it is dependent on the NLS settings.)

    Something like the following would handle the weekday, but you'll have to confirm the edge cases and check your NLS settings (for me, Monday is day 1 and Sunday is day 7):

    case
      when
        to_number(to_char(sysdate, 'D')) between 1 and 5 then
          case
            when to_char(trunc(sysdate, 'HH24') + 2/24, 'HH24') between '06' and '20'
                                                                                then 
              trunc(sysdate, 'HH24') + 2/24 
            else 
              trunc(sysdate)+1+(6/24)
            end
      else
        trunc(sysdate) + (8 - to_number(to_char(sysdate, 'D'))) + 6/24
    end 
    

    Although... think we are now firmly in the "time to right a function" stage. :)