oracle怎么计算两个日期之间去除周末的天数?
我有一张表有个日期字段,现在想计算当前系统时间减去该日期字段所得的工作天数,去除周六周日的,该怎么写?不要存储过程的,先谢谢各位~~...
我有一张表有个日期字段,现在想计算当前系统时间减去该日期字段所得的工作天数,去除周六周日的,该怎么写?不要存储过程的,先谢谢各位~~
展开
7个回答
展开全部
适用于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
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(*) 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 ('星期六', '星期日')
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 to_char(sysdate, 'day') from dual这个的运行结果
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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 ('星期六', '星期日')
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 ('星期六', '星期日')
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询