1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
CREATE
FUNCTION
[f_WorkDayADD](
@ date
datetime, --基础日期
@workday int
--要增加的工作日数
) 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 GO |