這個演算法還不夠嚴謹,沒有考慮到周
六、日調換假期等細節的情況。
1、建節假日表
create table [holiday] (
[id] [int] identity (1, 1) not null ,
[name] [nvarchar] (50) collate chinese_prc_ci_as not null ,
[begindate] [datetime] not null ,
[enddate] [datetime] not null ,
[adduser] [nvarchar] (50) collate chinese_prc_ci_as not null ,
[addtime] [datetime] not null ,
constraint [pk_holiday] primary key clustered
([id]
) on [primary]
) on [primary]
go2、計算工作日函式
create function [dbo].[workday]
(@beginday datetime,
@endday datetime
)returns int
asbegin
--set datefirst 1
declare @caldays int
declare @id int
select @caldays=0
while datediff(d, @beginday,@endday)>0
begin
select @beginday=dateadd(day,1,@beginday)
if datepart(dw,@beginday)>1 and datepart(dw,@beginday)<7
begin
select @id=count(*) from holiday where @beginday between begindate and dateadd(s,-1,dateadd(day,1,enddate))
if(@id=0)
select @caldays=@caldays+1
end
end
return @caldays
end3、推算指定日期前n個工作日的日期函式
create function [dbo].[begindate]
(@workday int ,
@endday datetime
)returns datetime
asbegin
declare @beginday datetime
declare @id int
select @beginday=@endday
while datediff(d, @beginday,@endday)<@workday
begin
select @beginday=dateadd(day,-1,@beginday)
if datepart(dw,@beginday)>1 and datepart(dw,@beginday)<7
begin
select @id=count(*) from holiday where @beginday between begindate and dateadd(s,-1,dateadd(day,1,enddate))
if(@id>0)
select @workday=@workday+1
endelse
begin
select @workday=@workday+1
end
endif(datepart(dw,@beginday)=2)
begin
select @beginday=dateadd(day,-2,@beginday)
end
return @beginday
end
SQL實現工作日計算
create function time span of minutes start day datetime,end day datetime returns float begin 返回按分鐘計算兩段時間的間隔,採用逐日靠近的方法求解,一天按8小時480分鐘計算,週末不計,不考慮法定節假日。如果...
計算工作日
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...