1. 程式人生 > >Oracle計算指定日期內的工作日(不包含週末)

Oracle計算指定日期內的工作日(不包含週末)

               
1、獲取當天是禮拜幾:select to_char(sysdate,'d') from dual;  --禮拜天為1,禮拜一為2,類推2、獲取 兩個時間段間的 工作日:select (trunc(&end_dt - &start_dt) -          ((case         WHEN (8 - to_number(to_char(&start_dt,'D'))) > trunc(&end_dt - &start_dt) + 1 THEN 0         ELSE          trunc((trunc(&end_dt - &start_dt) -                (8
- to_number(to_char(&start_dt,'D'))))/7) + 1 END) +          (case         WHEN mod(8 - to_char(&start_dt, 'D'), 7) > trunc(&end_dt - &start_dt) - 1 THEN 0         ELSE          trunc((trunc(&end_dt - &start_dt) - (mod(8 - to_char(&start_dt,'D'),7) + 1))/7) + 1  END)))         as
workingdays  from dual下面的sql可以直接執行:select (trunc(to_date('2010-11-11','yyyy-mm-dd') - to_date('2010-11-07','yyyy-mm-dd')) -          ((case         WHEN (8 - to_number(to_char(to_date('2010-11-07','yyyy-mm-dd'),'D'))) > trunc(to_date('2010-11-11','yyyy-mm-dd') - to_date('2010-11-07','yyyy-mm-dd')) + 1 THEN 0         ELSE
          trunc((trunc(to_date('2010-11-11','yyyy-mm-dd') - to_date('2010-11-07','yyyy-mm-dd')) -                (8 - to_number(to_char(to_date('2010-11-07','yyyy-mm-dd'),'D'))))/7) + 1 END) +          (case         WHEN mod(8 - to_char(to_date('2010-11-07','yyyy-mm-dd'), 'D'), 7) > trunc(to_date('2010-11-11','yyyy-mm-dd') - to_date('2010-11-07','yyyy-mm-dd')) - 1 THEN 0         ELSE          trunc((trunc(to_date('2010-11-11','yyyy-mm-dd') - to_date('2010-11-07','yyyy-mm-dd')) - (mod(8 - to_char(to_date('2010-11-07','yyyy-mm-dd'),'D'),7) + 1))/7) + 1  END)))         as workingdays  from dual