w********9 发帖数: 461 | 1 我抄了个function,就是计算n天以后的working day。现在有个holiday table tbl_
holiday,要把holiday排除在外,怎么加进去?
谢谢!
ALTER FUNCTION [dbo].[find_WorkingDayADD](
@date datetime, --start day
@workday int --days to be added
)RETURNS datetime
AS
BEGIN
DECLARE @bz int
--不是整周
SELECT @bz=CASE WHEN @workday<0 THEN -1 ELSE 1 END
,@date=DATEADD(Week,@workday/5,@date)
,@workday=@workday%5
-- 整周
WHILE @workday<>0
SELECT @date=DATEADD(Day,@bz,@date),
@workday=CASE WHEN (@@DATEFIRST+DATEPART(Weekday,@date)-1)%7 BETWEEN 1 AND 5
THEN @workday-@bz ELSE @workday END
--排除周末
WHILE (@@DATEFIRST+DATEPART(Weekday,@date)-1)%7 in(0,6)
SET @date=DATEADD(Day,@bz,@date)
RETURN(@date)
END | w********9 发帖数: 461 | 2 或者假设有个isholiday() function,可以判断出哪天是假日。
谢谢!
【在 w********9 的大作中提到】 : 我抄了个function,就是计算n天以后的working day。现在有个holiday table tbl_ : holiday,要把holiday排除在外,怎么加进去? : 谢谢! : ALTER FUNCTION [dbo].[find_WorkingDayADD]( : @date datetime, --start day : @workday int --days to be added : )RETURNS datetime : : : AS
| B*****g 发帖数: 34098 | 3 你水平不够的时候不要写太复杂的function,你需要把问题分成几个部分,我建议你这
么做
FUNCTION [dbo].[isWeekend](
@date datetime
)RETURNS bit -- 1for true, 0 for false
.....
FUNCTION [dbo].[isHoliday](
@date datetime
)RETURNS bit -- 1for true, 0 for false, check table
.....
FUNCTION [dbo].[isWorkingDay](
@date datetime
)RETURNS bit -- 1for true, 0 for false
As
Begin
if isWeekend(@date ) = 1 then
return 0
else if isHoliday(@date ) = 1 then
return 0
else
return 1
End
ALTER FUNCTION [dbo].[find_WorkingDayADD](
@date datetime, --start day
@workday int --days to be added, only consider positive for now
)RETURNS datetime
AS
Begin
while @workday >0
Begin
SET @date=DATEADD(Day,1,@date)
If isWorkingDay(@date ) = 1
set @workday = @workday -1
if @workday = 0
return @date
End
End
【在 w********9 的大作中提到】 : 我抄了个function,就是计算n天以后的working day。现在有个holiday table tbl_ : holiday,要把holiday排除在外,怎么加进去? : 谢谢! : ALTER FUNCTION [dbo].[find_WorkingDayADD]( : @date datetime, --start day : @workday int --days to be added : )RETURNS datetime : : : AS
|
|