当前位置:数据库 > Oracle >>

Oracle时间日期加减计算

上月末的日期:select last_day(add_months(sysdate, -1)) from dual;
本月的最后一秒:select trunc(add_months(sysdate,1),'mm') - 1/24/60/60 from dual
本周星期一的日期:select trunc(sysdate,'day')+1 from dual


年初至今的天数:select ceil(sysdate - trunc(sysdate, 'year')) from dual;

今天是今年的第几周 :select to_char(sysdate,'fmww') from dual
今天是本月的第几周:select to_char(sysdate,'ww') - to_char(trunc(sysdate,'mm'),'ww') + 1 as "weekofmon" from dual

本月的天数
select to_char(last_day(sysdate),'dd') days from dual
今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
下个星期一的日期
select next_day(sysdate,'monday') from dual


============================================

--计算工作日方法

create table t(s date,e date);
alter session set nls_date_format = 'yyyy-mm-dd';
insert into t values('2003-03-01','2003-03-03');
insert into t values('2003-03-02','2003-03-03');
insert into t values('2003-03-07','2003-03-08');
insert into t values('2003-03-07','2003-03-09');
insert into t values('2003-03-05','2003-03-07');
insert into t values('2003-02-01','2003-03-31');

-- 这里假定日期都是不带时间的,否则在所有日期前加trunc即可。
select s,e,e-s+1 total_days,
trunc((e-s+1)/7)*5 + length(replace(substr('01111100111110',to_char(s,'d'),mod(e-s+1,7)),'0','')) work_days
from t;

-- drop table t;

 

================================================================================

判断当前时间是上午下午还是晚上

select case
when to_number(to_char(sysdate,'hh24')) between 6 and 11 then '上午'
when to_number(to_char(sysdate,'hh24')) between 11 and 17 then '下午'
when to_number(to_char(sysdate,'hh24')) between 17 and 21 then '晚上'
end
from dual;

 

补充:数据库,Oracle教程
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,