How To Calculate Working Minutes in Without Week Off and Holidays SQL Server


       In this Section See about We to Calculate Working Times in Without WeekOffs and Non-Working Days. In this SQL Query, I mentioned in holiday Table as Leave Days.  You  Can Set the Work Start time and End Time In your Code. 

 Declare @StartDate as datetime
 set @StartDate ='2017-11-01 09:59:50.753'

 Declare @EndDate as datetime
 set @EndDate='2017-11-03 09:59:50.753'


       DECLARE @WorkStartTime TIME
       SET @WorkStartTime ='09:30'

    DECLARE @WorkEndTime TIME
    SET @WorkEndTime = '18:00'

 DECLARE @WorkingSecs BIGINT
    SET @WorkingSecs=0

    DECLARE @FirstDay DATE
    SET @FirstDay = CONVERT(DATE, @StartDate, 112)

    DECLARE @LastDay DATE
    SET @LastDay = CONVERT(DATE, @EndDate, 112)

    DECLARE @StartTime TIME
    SET @StartTime = CONVERT(TIME, @StartDate)

    DECLARE @FinishTime TIME
    SET @FinishTime = CONVERT(TIME, @EndDate)

    DECLARE @DailyWorkTime BIGINT
    SET @DailyWorkTime = DATEDIFF(MINUTE, @WorkStartTime, @WorkEndTime)

    IF (@StartTime<@WorkStartTime)
    BEGIN
        SET @StartTime = @WorkStartTime
    END
    IF (@FinishTime>@WorkEndTime)
    BEGIN
        SET @FinishTime=@WorkEndTime
    END
    IF (@FinishTime<@WorkStartTime)
    BEGIN
        SET @FinishTime=@WorkStartTime
    END
    IF (@StartTime>@WorkEndTime)
    BEGIN
        SET @StartTime = @WorkEndTime
    END

    DECLARE @CurrentDate DATE
    SET @CurrentDate = @FirstDay
    DECLARE @LastDate DATE
    SET @LastDate = @LastDay

    WHILE(@CurrentDate<=@LastDate)
    BEGIN  
           IF (DATEPART(dw, @CurrentDate)!=7 AND DATEPART(dw, @CurrentDate)!=1 and @CurrentDate
              not in (select mHolyDay.FestivalDate from mHolyDay))
        BEGIN
            IF (@CurrentDate!=@FirstDay) AND (@CurrentDate!=@LastDay)
            BEGIN
                SET @WorkingSecs = @WorkingSecs + @DailyWorkTime
            END
  
            ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate!=@LastDay)
            BEGIN
                SET @WorkingSecs = @WorkingSecs + DATEDIFF(SECOND, @StartTime, @WorkEndTime)
            END

            ELSE IF (@CurrentDate!=@FirstDay) AND (@CurrentDate=@LastDay)
            BEGIN
                SET @WorkingSecs = @WorkingSecs + DATEDIFF(SECOND, @WorkStartTime, @FinishTime)
            END
          
            ELSE IF (@CurrentDate=@FirstDay) AND (@CurrentDate=@LastDay)
            BEGIN
                SET @WorkingSecs = DATEDIFF(SECOND, @StartTime, @FinishTime)
            END
        END
        SET @CurrentDate = DATEADD(day, 1, @CurrentDate)
    END

    IF @WorkingSecs<0
    BEGIN
        SET @WorkingSecs=0
    END

       select @WorkingSecs
       SELECT CONVERT(varchar, DATEADD(ms, @WorkingSecs * 1000, 0), 114)



Comments

Popular posts from this blog

Unable to perform operation on the item is locked in workspace

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Insecure cookie setting: missing Secure flag