工作日计算

因工作需要,需要编写一个在指定日期后自动加上几天的计算工作日期,需要自动越过假期和周六、周日(上班还不能越过如2014-01-26日)

比如2014-09-25,

加1天就是2014-09-26,

加2天就是2014-09-29,

加3天就是2014-09-30,

加4天就是2014-10-09,

加5天就是2014-10-10,

加6天就是2014-10-13

网上有类似的基本都不能,只能自己写一个

 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

需要配合工作日历表来使用

/*==============================================================*/
/* 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
时间: 2024-08-06 20:00:15

工作日计算的相关文章

一个简单的工作日计算

一个简单的工作日计算: function getWorkDay(dtBegin:TDateTime;IncDays:Integer):TDateTime; const days: array[1..7] of string=('7','1','2','3','4', '5','6'); var week1:string; x:integer; begin //日期 x:=1; while (x<=IncDays) do begin //判断是否周末 week1:=days[DayOfWeek(d

工作日计算问题思路和实现

项目中眼下已有一周表存储了一年中全部的假日,给定查询起始日期和结束日期,推导出查询时间段内工作日是多少.为了简化这个问题,须要以下几个如果. 1. 不考虑周六周日,将其视作普通工作日 2. 假日没有交叠情况.不会出现10月1日到7日是国庆节,当中又有一个其他的节日 给出假日表的设计,某个假日都有起始时间和结束时间.这里仅仅取月日,这样就能表示每一年的假日. CREATE TABLE [dbo].[holiday]( [begin_time] [varchar](50) NULL, [end_ti

PostgreSQL 当月最后一天的工作日 , 计算日期是星期几

可以用pg自带函数select extract(dow from current_date),之所以没用主要是展示一下通过数学方法计算日期的原理. drop function if exists getDateWeek(date);drop function if exists intervalDay(date);drop function if exists getMonMaxDay(integer,integer);drop function if exists getMonMaxDate(

Java节假日算法

类:Vacation package test; import java.io.Serializable; import java.util.Date; public class Vacation implements Serializable { private static final long serialVersionUID = 1L; private Date date; private int days; public Date getDate() { return date; }

二、常用函数

(一)读写文件 (1)创建单位矩阵(主对角线元素均为1,其余元素均为0) i5 = eye(5) 注:eye()函数 numpy.eye(N,M=None, k=0, dtype=<type 'float'>) 关注第一个第三个参数就行了 第一个参数:输出方阵(行数=列数)的规模,即行数或列数 第三个参数:默认情况下输出的是对角线全"1",其余全"0"的方阵,如果k为正整数,则在右上方第k条对角线全"1"其余全"0"

SQL取出 所有周六 周日的日期

SQL取出 所有周六 周日的日期 create table SatSun([id] int identity(1,1),[date] datetime,[weekday] char(6)) go declare @datetime datetime,@weekday char(6) set @datetime='2007-1-1' while @datetime<='2007-12-31' begin select @weekday=datename(weekday,@datetime) if

国产Linux饭局有多少?

国产Linux饭局有多少? 请看下图: 上图是新京报5月13日刊出的一篇文章所引用的一副图画,图的下方标注文字是:"中国特色官场饭局",该文标题为"中国官场公款饭局揭秘:每天82万个1年花3千亿". 该文说,根据2014年3月国家统计局财务司司长张仲梁主持的一项调查表明:2012年,我国中央政府司局级干部平均每周有1.1次饭局,省政府的司局级干部平均每周有1.3次饭局.到了基层,花在饭局上的时间就更长了:市长平均每周15.1次.县长则达到了每周18.2次,按一周五个

Python逻辑回归原理及实际案例应用

前言 上面我们介绍了线性回归, 岭回归, Lasso回归, 今天我们来看看另外一种模型-"逻辑回归". 虽然它有"回归"一词, 但解决的却是分类问题 目录 1. 逻辑回归 2. 优缺点及优化问题 3. 实际案例应用 4. 总结 正文 在前面所介绍的线性回归, 岭回归和Lasso回归这三种回归模型中, 其输出变量均为连续型, 比如常见的线性回归模型为: 其写成矩阵形式为: 现在这里的输出为连续型变量, 但是实际中会有"输出为离散型变量"这样的需求,

数据之路 - Excel函数

日期与时间函数 函数名 语法 函数功能 备注 DATE date(year,month,day)     EDATE edate(start_date,months)     DATEVALUE datevalue(date_text)     YEAR year(serial_number)     MONTH month(serial_number)     DAY day(serial_number)     HOUR hour(serial_number)     MINUTE minu