create function time_span_of_minutes(
start_day datetime,
end_day datetime)
returns float
begin
-- 返回按分鐘計算兩段時間的間隔,採用逐日靠近的方法求解,一天按8小時480分鐘計算,週末不計,不考慮法定節假日。
-- 如果起始日期在週末,則計算當天時間。
declare minutes float;
declare next_day datetime;
set minutes=0.0;
set next_day=start_day;
if(timestampdiff(day,start_day,end_day)<1.0) then
set minutes=minutes+timestampdiff(minute,start_day,end_day);
else
while timestampdiff(day,next_day,end_day)>=1.0 do
if ((dayofweek(next_day)=7) or (dayofweek(next_day)=1)) then
set next_day=next_day+interval 1 day;
else
set next_day=next_day+interval 1 day;
set minutes=minutes+480.0;
end if;
end while;
set minutes=minutes+timestampdiff(minute,next_day,end_day);
if ((dayofweek(start_day)=7) or (dayofweek(start_day)=1)) then
set minutes=minutes+timestampdiff(minute,start_day,convert(concat(substring(start_day from 1 for 10),' 17:30:00'),datetime));
end if;
end if;
return minutes;
end;
create function time_span_of_minutes(
start_day datetime,
end_day datetime)
returns float
begin
-- 返回按分鐘計算兩段時間的間隔,採用逐日靠近的方法求解,一天按8小時480分鐘計算,週末不計,不考慮法定節假日。
-- 如果起始日期在週末,則計算當天時間。
declare minutes float;
declare next_day datetime;
set minutes=0.0;
set next_day=start_day;
if(timestampdiff(day,start_day,end_day)<1.0) then
set minutes=minutes+timestampdiff(minute,start_day,end_day);
else
while timestampdiff(day,next_day,end_day)>=1.0 do
if ((dayofweek(next_day)=7) or (dayofweek(next_day)=1)) then
set next_day=next_day+interval 1 day;
else
set next_day=next_day+interval 1 day;
set minutes=minutes+480.0;
end if;
end while;
set minutes=minutes+timestampdiff(minute,next_day,end_day);
if ((dayofweek(start_day)=7) or (dayofweek(start_day)=1)) then
set minutes=minutes+timestampdiff(minute,start_day,convert(concat(substring(start_day from 1 for 10),' 17:30:00'),datetime));
end if;
end if;
return minutes;
end;
沒考慮中午休息時間
摘自 jasper鍵盤舞步
sql計算工作日函式
這個演算法還不夠嚴謹,沒有考慮到周 六 日調換假期等細節的情況。1 建節假日表 create table holiday id int identity 1,1 not null name nvarchar 50 collate chinese prc ci as not null begindat...
計算工作日
vb vba中計算工作日 僅剔除星期 六 日 的函式。function getworkdaycount startday as date,endday as date as integer dim nums as integer,numy as integer dim weekdaynumy 7,6...
動態計算工作日
由table來設定是否為工作日,如果為節假日則設定為非工作日,即talbe會設定每天是否為工作日 帶入的日期必須為工作日。alter function dbo fn workdayadd date datetime days int returns datetime nvarchar 100 asb...