在Oracle中實現時間相加處理

2021-09-12 05:51:58 字數 1791 閱讀 8227

在oracle中實現時間相加處理

-- 名稱:add_times

-- 功能:返回d1與newtime相加以後的結果,實現時間的相加

-- 說明:對於newtime中的日期不予考慮

-- 日期:

-- 版本:1.0

create or replace function add_times(d1 in date,newtime in date) return date

ishh  number;

mm  number;

ss  number;

hours number;

dresult date;

begin

-- 下面依次取出時、分、秒

select to_number(to_char(newtime,'hh24')) into hh from dual;

select to_number(to_char(newtime,'mi')) into mm from dual;

select to_number(to_char(newtime,'ss')) into ss from dual;

-- 換算出newtime中小時總和,在一天的百分幾

hours := (hh + (mm / 60) + (ss / 3600))/ 24;

-- 得出時間相加後的結果

select d1 + hours into dresult from dual;

return(dresult);

end add_times;

create or replace function get_workingdays(

ny in varchar2

) return integer is

函式名稱:get_workingdays

中文名稱:求某一年月中共有多少工作日

作者姓名:

輸入引數:ny:所求包含工作日數的年月,格式為yyyymm,如200405

返 回 值:整型值,包含的工作日數目。

演算法描述:使用last_day函式計算出引數所給年月共包含多少天,根據這個值來構造乙個迴圈。在這個迴圈中先求這個月的每一天與乙個已知是星期天的日期(2001-12-30是星期天)的差,所得的差再對7求模。如果所求日期在2001-12-30以前,那麼所得的差既是負數,求模後所得值範圍為大於-6,小於0,如-1表示星期六,故先將求模的結果加7,再求7的模. 如過所得值不等於0和6(即不是星期六和星期天),則算乙個工作日。

result integer := 0;

myts integer;     --所給年月的天數

scts integer;     --某天距2001-12-30所差的天數

rq  date;

djt integer := 1;  --

begin

myts := to_char(last_day(to_date(ny,'yyyymm')),'dd');

loop

rq := to_date(ny||substr(100+djt,2),'yyyymmdd');

scts := rq - to_date('2001-12-30','yyyy-mm-dd');

if mod(mod(scts,7)+7,7) not in(0,6) then

result := result + 1;

end if;

djt := djt + 1;

exit when djt>myts;

end loop;

return result;

end get_workingdays;

在Oracle中實現時間相加處理

在oracle中實現時間相加處理 名稱 add times 功能 返回d1與newtime相加以後的結果,實現時間的相加 說明 對於newtime中的日期不予考慮 日期 版本 1.0 create or replace function add times d1 in date,newtime in...

HTML中如何實現時間在頁面中實時更新

doctype html utf 8 time title var date newdate var year date.getfullyear var month date.getmonth 1 month month 10?0 month month var day date.getdate d...

android app中實現時間展示樣式

這裡做螢幕輸出,比較新聞的上傳時間和當前時間差值,實現,剛剛,多少分鐘前,多少小時前發布功能這裡只寫了主要 dateformat sf3 new dateformat yyyy mm dd hh mm ss date newsdate ne.getnewsdate date nowdate new ...