亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算兩個日期之間的工作日

計算兩個日期之間的工作日

滄海一幻覺 2019-06-19 11:11:23
計算兩個日期之間的工作日如何計算SQLServer中兩個日期之間的工作日數?星期一到星期五,一定是T-SQL。
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

對于工作日(星期一至星期五),只需進行一次選擇,如下所示:

DECLARE @StartDate DATETIMEDECLARE @EndDate DATETIMESET @StartDate = '2008/10/01'SET @EndDate = '2008/10/31'SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)

如果你想包括假期,你得想辦法.


查看完整回答
反對 回復 2019-06-19
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

都歸功于博格丹·馬克西姆和彼得·莫滕森。這是他們的帖子,我剛剛將假日添加到函數中(假設您有一個表“tblholidays”,其中有一個datetime字段“HolDate”)。

--Changing current database to the Master database allows function to be shared by everyone.USE MASTER
GO--If the function already exists, drop it.IF EXISTS(
    SELECT *
    FROM dbo.SYSOBJECTS    WHERE ID = OBJECT_ID(N'[dbo].[fn_WorkDays]')
    AND XType IN (N'FN', N'IF', N'TF'))DROP FUNCTION [dbo].[fn_WorkDays]GO CREATE FUNCTION dbo.fn_WorkDays--Presets--Define the input 
    parameters (OK if reversed by mistake).(
    @StartDate DATETIME,
    @EndDate   DATETIME = NULL --@EndDate replaced by @StartDate when DEFAULTed)--Define the output data type.RETURNS INTAS--Calculate
     the RETURN of the function.BEGIN
    --Declare local variables
    --Temporarily holds @EndDate during date reversal.
    DECLARE @Swap DATETIME    --If the Start Date is null, return a NULL and exit.
    IF @StartDate IS NULL
        RETURN NULL

    --If the End Date is null, populate with Start Date value so will have two dates (required by DATEDIFF below).
    IF @EndDate IS NULL
        SELECT @EndDate = @StartDate   
         --Strip the time element from both dates (just to be safe) by converting to whole days and back to a date.
    --Usually faster than CONVERT.
    --0 is a date (01/01/1900 00:00:00.000)
    SELECT @StartDate = DATEADD(dd,DATEDIFF(dd,0,@StartDate), 0),
            @EndDate   = DATEADD(dd,DATEDIFF(dd,0,@EndDate)  , 0)

    --If the inputs are in the wrong order, reverse them.
    IF @StartDate > @EndDate        SELECT @Swap      = @EndDate,
               @EndDate   = @StartDate,
               @StartDate = @Swap    --Calculate and return the number of workdays using the input parameters.
    --This is the meat of the function.
    --This is really just one formula with a couple of parts that are listed on separate lines for documentation purposes.
    RETURN (
        SELECT
        --Start with total number of days including weekends
        (DATEDIFF(dd,@StartDate, @EndDate)+1)
        --Subtact 2 days for each full weekend
        -(DATEDIFF(wk,@StartDate, @EndDate)*2)
        --If StartDate is a Sunday, Subtract 1
        -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday'
            THEN 1
            ELSE 0
        END)
        --If EndDate is a Saturday, Subtract 1
        -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday'
            THEN 1
            ELSE 0
        END)
        --Subtract all holidays
        -(Select Count(*) from [DB04\DB04].[Gateway].[dbo].[tblHolidays]
          where  [HolDate] between @StartDate and @EndDate )
        )
    END  GO-- Test Script/*
declare @EndDate datetime= dateadd(m,2,getdate())
print @EndDate
select  [Master].[dbo].[fn_WorkDays] (getdate(), @EndDate)
*/


查看完整回答
反對 回復 2019-06-19
  • 3 回答
  • 0 關注
  • 778 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號