适用于SQL上下文环境
去掉的是周六/7,周日/1两天周末
具体SQL如下、这是避免日期格式本地化的版本
select count(1) from (
select to_char(TO_DATE('2016-12-24','yyyy-MM-dd') - level, 'd') DOW
from dual
connect by level <= trunc(TO_DATE('2016-12-24','yyyy-MM-dd') - TO_DATE('2016-11-19','yyyy-MM-dd'))
) where DOW not in (7, 1);
总结:
第一层查询是通过connect by level生成两个日期间的天数行
其中to_char(,'d')DOW字段返回的是周几的对应1~7的数字
第二层查询就是去掉周六周日然后count天数
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
select count(*) from table_a where to_char(date,'day') not in('星期六','星期天')
或者
select count(*) from table_a where to_char(date,'d') not in('1','7')
select count(1)
from (select to_char(sysdate - level, 'day') dayofweek
from dual
connect by level <=
(select trunc(sysdate - to_date('&date', 'yyyy-MM-dd'))
from dual))
where dayofweek not in ('星期六', '星期日')
select count(1)
from (select to_char(hiredate - level, 'day') dayofweek
from emp connect by level <=
(select trunc(hiredate - to_date('&date', 'yyyy-MM-dd'))
from dual))
where dayofweek not in ('星期六', '星期日')