获取工作日

  需要编写一个SqlServer函数,F_getWorkday,传入两个参数,第一个为时间date,第二个参数为第几个工作日num。调用F_getWorkday后返回date之后的第num个工作日。

  现有节假日安排表如下:

  

 drop function [dbo].[F_getWorkday];
create function [dbo].[F_getWorkday](
@staDate datetime,
@workDay int
)
returns datetime
as
begin
declare @curDay int ;
declare @tempDay datetime;
declare @weekday int;
declare @endDate datetime;
set @curDay=1;
select @tempDay = dateadd(day,1,@staDate);
while @curDay <=@workDay
begin
declare @tempDay2 datetime; --没有放假
if(select COUNT(1) from dategrid where set_type=1 and begin_date<=@tempDay and end_date>=@tempDay)<=0
begin--没加班
if(select COUNT(1) from dategrid where set_type=2 and begin_date<=@tempDay and end_date>=@tempDay)<=0
begin
select @weekday = datepart(weekday, @tempDay);
if (@weekday=1)--星期天
begin
select @tempDay = dateadd(day,1,@tempDay);
end
else if(@weekday>=2 and @weekday<=6)--周一到周五
begin
set @endDate = @tempDay;
select @tempDay = dateadd(day,1,@tempDay);
set @curDay = @curDay + 1;
end
else--周六
begin
select @tempDay = dateadd(day,2,@tempDay);
end;
end
else--加班
begin
set @tempDay2 = null;
select @tempDay2 = end_date from dategrid where set_type=2 and begin_date<=@tempDay and end_date>=@tempDay;--@tempDay在加班中的最后一天
set @endDate = @tempDay2;
select @tempDay = dateadd(day,1,@tempDay2);
set @curDay = @curDay + 1;
end;
end
else--放假
begin
select @tempDay2 = end_date from dategrid where set_type=1 and begin_date<=@tempDay and end_date>=@tempDay;--@tempDay在放假中的最后一天
select @tempDay = dateadd(day,1,@tempDay2);
set @endDate =@tempDay;--最后一天在假期情况
set @curDay = @curDay + 1;--叠加器加一次
end;
end;
return @endDate;
end

SqlServer函数获取指定日期后的第某个工作日的更多相关文章

  1. ThinkPHP 获取指定日期后第N个工作日具体日期

    思路: 1.获取到查询年份内所有工作日数据数组2.获取到查询开始日期在工作日的索引3.计算需查询日期索引4.获得查询日期 /*创建日期类型记录表格*/ CREATE TABLE `tb_workday ...

  2. ThinkPHP中如何获取指定日期后工作日的具体日期

    思路: 1.获取到查询年份内所有工作日数据数组2.获取到查询开始日期在工作日的索引3.计算需查询日期索引4.获得查询日期 /*创建日期类型记录表格*/ CREATE TABLE `tb_workday ...

  3. mssql sqlserver 获取指定日期为本月第几个工作日

    转自:http://www.maomao365.com/?p=6771 摘要: 下文讲述工作中,需要获取指定日期在当月的工作日 下文讲述常规的指定工作日所在月的天数分析,实现思路:1 生成一个国家法定 ...

  4. php获取指定日期所在星期的开始时间与结束时间

    function getWeekRange($date){    $ret=array();    $timestamp=strtotime($date);    $w=strftime('%u',$ ...

  5. php获取指定日期的前一天,前一月,前一年日期

    ## php获取指定日期的前一天,前一月,前一年日期   前一天的日期为: date("Y-m-d",strtotime("-1 days",strtotime ...

  6. 获取指定日期相关DATENAME和DATEPART数据

    DATENAME和DATEPART有何区别,Insus.NET写成一个函数,可以方便查询与对比: 一个是返回一个字符串,另一个是返回一个整数. SET ANSI_NULLS ON GO SET QUO ...

  7. C#获取指定日期为一年中的第几周

    /// <summary> /// 获取指定日期,在为一年中为第几周 /// </summary> /// <param name="dt">指 ...

  8. MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天

    ufn_GetWeekFirstAndEndDay    获取指定日期所在星期的第一天和最后一天日期 ALTER FUNCTION [dbo].[ufn_GetWeekFirstAndEndDay]( ...

  9. PHP:获取指定日期所在月的开始日期与结束日期

    /** * 获取指定日期所在月的开始日期与结束日期 * @param string $date * @param boolean 为true返回开始日期,否则返回结束日期 * @return arra ...

随机推荐

  1. iOS-控件响应用户控制事件之事件处理

    事件处理 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication.UIViewControlle ...

  2. 慎将MBTI测试用于招聘或就业:4星|《人格魅力修炼指南》

    人格魅力修炼指南:成为理想中的自己,就靠它了!(<哈佛商业评论>增刊) <哈佛商业评论>的11篇领导者人格魅力相关的文章.比较专业. 一些重要的信息:慎将MBTI测试用于“招聘 ...

  3. C# 获取文件编码

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  4. Java软件开发不同薪资级别-技术要求

    15~20万 WEB应用服务器(Tomcat.Weblogic.Jetty.JBoss.WebSphere) NoSQL(Redis.MongoDB.HBase.Memcache) 消息中间件(Kaf ...

  5. cce - 控制台中文环境

    语法 (SYNTAX) cce [-e program] 描述 (DESCRIPTION) 该程序是一个类似于 WZCE , yact 和 chdrv 的控制台中文平台.进入该环境后可以用“空格 + ...

  6. log4net小记

    log4net添加: Install-Package Log4net log4net.config配置: <?xml version="1.0" encoding=" ...

  7. webgl推荐书籍

    网址:https://www.douban.com/doulist/45940373/ webgl 来自: Pasu2017-04-17创建   2017-07-25更新   推荐 关注 2 人关注 ...

  8. ZXing.dll 生成二维码 C# winform net4.5

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. CAD控件使用教程 自定义实体的实现

    自定义实体的实现 1 .       自定义实体... 3 1.1      说明... 3 1.2      类的类型信息... 3 1.3      worldDraw.. 4 1.4      ...

  10. sql server update+select(子查询修改)20190304

    if OBJECT_ID('tempdb..##t2') is not null drop table ##t2;create table ##t2( a int, b int, c datetime ...