1

I am trying to make a holiday booking application and it runs of a JQuery UI calendar. the star and end date they select are passed to SQL where it puts the holiday into the requested table. the only problem is if i put a start and an end date in, it doesn't take into account the weekends. each user of the system has 21 days allowance off per calendar year and it deducts the weekends of this also. here is the current code.

SET @DateDiff = (DATEDIFF(Day,@StartDate,@EndDate) +1)

Above is the date-diff statement and below is the insertion code

BEGIN
    INSERT INTO Holidays(EmployeeID, StartDate, EndDate, Duration,Status)
    VALUES(@EmployeeID, @StartDate, @EndDate, @DateDiff,'PE')

    UPDATE Employees
    SET AnnualDaysHolidayAllowance = AnnualDaysHolidayAllowance - @DateDiff
    WHERE ID = @EmployeeID
    SELECT 'successful' AS result
END

There is more code for the business checks and rules but this is the only part i need to try and fix. i am relatively new to coding so if possible please leave an explanation to help me understand the fix, thanks.

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
Kieranmv95
  • 828
  • 4
  • 14
  • 31

1 Answers1

1

Found the solution it was on the link posted. source: Count work days between two dates The answer (not my own work)

    SET @DateDiff = (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))
Community
  • 1
  • 1
Kieranmv95
  • 828
  • 4
  • 14
  • 31