Skip to main content

Posts

Showing posts from July, 2022

Get the number of work days Mon to Friday from a start and end date

 I found the following function helpful when trying to figure out the days worked during COVID lock down. -- ============================================= -- Author: KrishanG -- Create date: 20/07/2022 -- Description: Get the number of working days between two dates includes these dates as well -- specify the date time as -- SET @StartDate = '2021/07/16' -- SET @EndDate = '2021/07/27' -- ============================================= CREATE FUNCTION [dbo].[fnGetWeekdays] ( -- Add the parameters for the function here @StartDate datetime, @EndDate datetime ) RETURNS int AS BEGIN -- Declare the return variable here DECLARE @Result int -- DECLARE @StartDate DATETIME -- DECLARE @EndDate DATETIME -- SET @StartDate = '2021/07/16' -- SET @EndDate = '2021/07/27' SELECT @Result= (DATEDIFF(dd, @StartDate, @EndDate) + 1) -(DATEDIFF(wk, @StartDate, @EndDate) * 2) -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) -