• sql server:自定義計算固定工作日,雙休日函數


    ---sql server 
    declare @date datetime
    set @date='2012-02-03'--getdate()
    --本月第一天
    SELECT DATEADD(mm, DATEDIFF(mm,0,@date), 0)
    --本月最后一天
    SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,@date)+1, 0))
    
    
    ---有個月多少天函數
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAMonthHowNumber]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[GetAMonthHowNumber]
    GO
    CREATE   function  GetAMonthHowNumber
    (  
    	@date datetime
    )
    returns int
    as
    begin
    	declare @int int
    	select @int=datediff(dd , @date , dateadd(mm, 1, @date))
    	return @int
    end
    GO
    
    --
    select [dbo].[GetAMonthHowNumber] (getdate()) AS '月天數'
    
    
    ---計算當月周六,周日有多少天
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAMonthStatSunNumber]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[GetAMonthStatSunNumber]
    GO
    CREATE   function  GetAMonthStatSunNumber
    (  
    	@date datetime
    )
    returns int
    as
    begin
    declare @Sdate datetime ,@Edate datetime
    
    SELECT @Sdate=DATEADD(mm, DATEDIFF(mm,0,@date), 0)
    SELECT @Edate=dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,@date)+1, 0))
    --set @Sdate='2013-05-01'
    --set @Edate='2013-05-31'
    declare @aa table (strdate datetime)  
    declare @i int  
    set @i=datediff(day,@Sdate,@Edate)  
    while(@i>=0)  
    begin  
    insert @aa  values (dateadd(day,@i,@Sdate))  
    set @i=@i-1  
    end  
    select @i= count(*)  from @aa where   datepart(weekday,strdate) in (1,7)  --not in (1,7)
    return @i
    end
    go
    
    --
    select [dbo].[GetAMonthStatSunNumber] (getdate()) AS '雙休日天數'
    
    
    
    --計算當月除周六,周日有多少天,也是有多少工作日
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAMonthWorkDayNumber]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[GetAMonthWorkDayNumber]
    GO
    CREATE   function  GetAMonthWorkDayNumber
    (  
    	@date datetime
    )
    returns int
    as
    begin
    declare @Sdate datetime ,@Edate datetime
    
    SELECT @Sdate=DATEADD(mm, DATEDIFF(mm,0,@date), 0)
    SELECT @Edate=dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,@date)+1, 0))
    --set @Sdate='2013-05-01'
    --set @Edate='2013-05-31'
    declare @aa table (strdate datetime)  
    declare @i int  
    set @i=datediff(day,@Sdate,@Edate)  
    while(@i>=0)  
    begin  
    insert @aa  values (dateadd(day,@i,@Sdate))  
    set @i=@i-1  
    end  
    select @i= count(*)  from @aa where   datepart(weekday,strdate) not in (1,7)  --not in (1,7)
    return @i
    end
    go
    
    ---
    select [dbo].[GetAMonthWorkDayNumber] (getdate()) as '工作日天數'
    
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    8月7号的练习:HDU 1069&&POJ 1636&&HDU 1031&&HDU 1051&&HDU 1551
    8月8号的线段树:HDU 1754&&POJ 3264&&HDU1166
    8月6号的题目:HDU 1003&& POJ 1050&&HDU 1800&&HDU 2036&& POJ 1088(记忆化搜索)
    HDU 1052
    背包问题九讲:
    一个人的旅行 HDU 2066 &&HDU Today HDU 2112
    8月3号的LCS,LIS,LICS:Longest Ordered Subsequence&&Common Subsequence&&Greatest Common Increasing Subsequence
    那些操蛋的搜索题目:逃离迷宫&&哈密顿绕行世界问题
    C语言栈调用机制初探
    linux0.11改进之四 基于内核栈的进程切换
  • 原文地址:https://www.cnblogs.com/geovindu/p/3105369.html
Copyright © 2020-2023  润新知