这两天领导让统计日终执行情况,对时间进行了很多的处理
现在总结一下:
1. trunc(date, fmt)
1) 去年的前两位加以: 2010 得到的是2001-1-1
SELECT trunc(sysdate,'CC') FROM dual;
2) 得到今年日期的第一天
SELECT trunc(sysdate,'YYYY') FROM dual;
3)得到季度的第一天
SELECT trunc(sysdate,'Q') FROM dual;
4,)IY 能得到去年年底(俺没有实验成功,以后再研究)
5)MM 取月的第一天
6)DD 取当前日期,与没有参数相同
7)IW Same day of the week as the first day of the ISO year
WW W 得到的是周六
8)HH24 MI 得到小时数,得到分钟数
2. 日期相减
2. 日期相减的一个函数
create or replace function subtract_time(start_time in date ,end_time in date )
return varchar2
as
---s_time date := to_date('2010-3-2 18:29:30', 'yyyy-mm-dd hh24:mi:ss');
--- e_time date := to_date('2010-3-3 0:48:41', 'yyyy-mm-dd hh24:mi:ss');
s_time date := start_time;
e_time date := end_time;
ss_sum integer := 0;
ss integer := 0;
mm integer := 0;
hh integer := 0;
begin
ss_sum := (e_time - s_time) * 24 * 60 * 60;
ss := mod(ss_sum,60);
mm := mod((ss_sum - ss)/60, 60);
hh :=(ss_sum - ss - 60 * mm)/3600;
return (lpad(hh,2,'0')||':'||lpad(mm,2,'0')||':'||lpad(ss,2,'0'));
end;
3.把字符串时间转化成秒
create or replace function time2sec(str_time in varchar2 )
return integer
as
---s_time date := to_date('2010-3-2 18:29:30', 'yyyy-mm-dd hh24:mi:ss');
--- e_time date := to_date('2010-3-3 0:48:41', 'yyyy-mm-dd hh24:mi:ss');
s_time varchar2(9) := str_time;
l_index integer := 0;
hh_sum integer := 0;
mm_sum integer := 0;
ss_sum integer := 0;
---ss_sum integer := 0;
ss integer := 0;
mm integer := 0;
hh integer := 0;
begin
select instr(s_time,':') into l_index from dual;
if l_index = 0 then
dbms_output.put_line( 'para format is error');
return null;
end if;
hh_sum := to_number(substr(s_time, 1,l_index-1)) * 60 * 60;
mm_sum := to_number(substr(s_time, l_index+1,2)) * 60;
return (hh_sum + mm_sum + to_number(substr(s_time,l_index+4,2)));
---- display the time
/* ss := mod(ss_sum,60);
mm := mod((ss_sum - ss)/60, 60);
hh :=(ss_sum - ss - 60 * mm)/3600;
return (lpad(hh,2,'0')||':'||lpad(mm,2,'0')||':'||lpad(ss,2,'0'));*/
end;
2.把秒转化成字符串时间
create or replace function sec2time(l_time in integer )
return varchar2
as
---s_time date := to_date('2010-3-2 18:29:30', 'yyyy-mm-dd hh24:mi:ss');
--- e_time date := to_date('2010-3-3 0:48:41', 'yyyy-mm-dd hh24:mi:ss');
ss_sum integer := l_time;
---ss_sum integer := 0;
ss integer := 0;
mm integer := 0;
hh integer := 0;
begin
---- display the time
ss := mod(ss_sum,60);
mm := mod((ss_sum - ss)/60, 60);
hh :=(ss_sum - ss - 60 * mm)/3600;
return (lpad(hh,2,'0')||':'||lpad(mm,2,'0')||':'||lpad(ss,2,'0'));
end;
4.这样可以对时间求平均值
select cs1.sec2time(avg(cs1.time2sec(s_time)))
有点傻,但是能用哈哈,AVG可以换成sum等你想用的东东
5.数据库本省支持日期的相应操作
select
case when a.brcd = BBBBthen '滨海'
when a.brcd = CCCC then '望京'
when a.brcd = DDDD then '五道口'
when a.brcd = EEEE then '北京'
when a.brcd = FFFF then '天津'
when a.brcd = GGGG then '大连'
when a.brcd = HHHH then '上海'
when a.brcd = PPPP then 'H Q '
when a.brcd is null then '总时间 '
end brcd,
min(S_TIME) s_time,max(E_TIME) e_time ,cs1.subtract_time( min(S_TIME),max(E_TIME)) time ---, to_char(sysdate,'yyyy-mm-dd') nextday
from
(select * from race.t_cob_res where flag=2) a
where
a.trdt between '20110104' and '20110104' and
---a.trdt = '20110128' and
a.brcd in (select brcd from tbcs_brcd where roccd = AAAA)
group by rollup(a.brcd)
6. 取date日期的时间部分
select to_char(sysdate, 'HH24:mi:ss') from dual;
7.oracle 中换行chr(13)chr(10)
或者用脚本命令:awk '{ gsub(/|/, "\n"); print }' $1
8. 求时间的平均值,sum值也可以弄到excel中,那里面也有一些函数
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21634752/viewspace-687098/ ,如需转载,请注明出处,否则将追究法律责任。