工做日計算

因工做須要,須要編寫一個在指定日期後自動加上幾天的計算工做日期,須要自動越過假期和周6、週日(上班還不能越過如2014-01-26日)spa

好比2014-09-25,code

加1天就是2014-09-26,blog

加2天就是2014-09-29,ip

加3天就是2014-09-30,io

加4天就是2014-10-09,table

加5天就是2014-10-10,class

加6天就是2014-10-13sed

網上有相似的基本都不能,只能本身寫一個object

 1 print 'compiling procedure dbo.workday_add ...'
 2 
 3 if object_id('dbo.workday_add' ,'p') is not null
 4     drop procedure dbo.workday_add
 5 go
 6 
 7 create procedure dbo.workday_add(
 8      @rtncode      varchar(5) output
 9     ,@rtnmsg       varchar(255) output
10     ,@begindate    datetime           --開始日期
11     ,@workday      int                --要增長的工做日數
12     ,@closedate    date output        --截止日期
13 )
14 as
15 /******************************************************************
16 項目名稱:xxxxx管理平臺
17 所屬模塊:xxxxx
18 概要說明:
19     中文名稱:返回截止時間
20     用途簡述:返回截止時間
21 語法信息:
22     輸入參數:
23              @begindate  --開始日期
24              @workday    --要增長的工做日數
25     輸出參數:
26              @closedate --截止日期
27     調用舉例:
28              declare @rtncode varchar(5),@rtnmsg varchar(255),@closedate date
29              exec dbo.workday_add @rtncode output,@rtnmsg output,'2014-09-25',1,@closedate output
30              select @rtncode,@rtnmsg,@closedate
31 修訂記錄:
32     修訂日期    修訂人     修改內容簡要說明
33     ----------  ---------  ------------------------------
34     2014-09-09  趙文彬     建立
35 ******************************************************************/
36 begin
37     set nocount on
38     set datefirst 1
39     set @rtncode = '0000'
40     set @rtnmsg = 'successful.'
41 
42     begin try
43         declare @enddate datetime,@dw int
44         declare @jia int,@ban int,@work_date datetime
45         select @jia=0,@ban=0
46 
47         while @workday>0
48         begin
49             select @dw=datepart(dw, @begindate+1),@enddate=@begindate+1
50             if exists(select 1 from dbo.work_time where convert(varchar(10),work_date,101)= convert(varchar(10),@begindate+1,101))
51             begin
52             select top 1 @jia=case when [type]=1 then 1 else 0 end
53                         ,@ban=case when [type]=2 then 1 else 0 end
54             from dbo.work_time where convert(varchar(10),work_date,101)= convert(varchar(10),@begindate+1,101)
55             end
56             else
57                 begin
58                     select @jia=0,@ban=0
59                 end
60             if (@dw=1 and @jia=0) or (@dw=2 and @jia=0) or (@dw=3 and @jia=0) or (@dw=4 and @jia=0) or (@dw=5 and @jia=0) or (@dw=6 and @ban=1) or (@dw=7 and @ban=1)
61             begin
62                 select @begindate=@begindate+1,@workday=@workday-1
63             end
64             else
65                 begin
66                     select @begindate=@begindate+1
67                 end
68         end
69         select @closedate = convert(varchar(10),@enddate,120)
70     end try
71     begin catch
72         select @rtnmsg = error_message()
73                ,@rtncode = error_number()
74         return
75     end catch
76 end
77 go
78 
79 if @@error<>0
80     print 'error-----dbo.workday_add-----error'
81 else
82     print 'compiled procedure dbo.workday_add'
83 go

須要配合工做日曆表來使用date

/*==============================================================*/
/* Table: WORK_TIME                                             */
/*==============================================================*/
PRINT 'dbo.WORK_TIME'
GO

if object_id('dbo.WORK_TIME', 'U') IS NOT NULL 
   drop table dbo.WORK_TIME

create table dbo.WORK_TIME (
   WORK_DATE            DATE                 not null,
   TYPE                 INT                  not null default 1,
   constraint PK_WORK_TIME primary key (WORK_DATE)
)
go

execute sp_addextendedproperty 'MS_Description', '工做日曆', 'schema', 'dbo', 'table', 'WORK_TIME'
go

execute sp_addextendedproperty 'MS_Description', '日期', 'schema', 'dbo', 'table', 'WORK_TIME', 'column', 'WORK_DATE'
go

execute sp_addextendedproperty 'MS_Description', '工做日類型(1-假 2-班)', 'schema', 'dbo', 'table', 'WORK_TIME', 'column', 'TYPE'
go

最後放上2014年的工做日曆

truncate table dbo.work_time;

insert into dbo.work_time(work_date,type)
           select '2014-01-01',1
union all select '2014-01-26',2
union all select '2014-01-30',2
union all select '2014-01-31',1
union all select '2014-02-01',1
union all select '2014-02-02',1
union all select '2014-02-03',1
union all select '2014-02-04',1
union all select '2014-02-05',1
union all select '2014-02-06',1
union all select '2014-02-07',2
union all select '2014-02-08',2
union all select '2014-04-05',1
union all select '2014-04-06',1
union all select '2014-04-07',1
union all select '2014-05-01',1
union all select '2014-05-02',1
union all select '2014-05-03',1
union all select '2014-05-04',2
union all select '2014-05-31',1
union all select '2014-06-01',1
union all select '2014-06-02',1
union all select '2014-09-06',1
union all select '2014-09-07',1
union all select '2014-09-08',1
union all select '2014-10-01',1
union all select '2014-10-02',1
union all select '2014-10-03',1
union all select '2014-10-04',1
union all select '2014-10-05',1
union all select '2014-10-06',1
union all select '2014-10-07',1
union all select '2014-10-08',1
相關文章
相關標籤/搜索