Het bericht Calculate DateDiff With Only WeekDays or Workdays verscheen eerst op SQLBlog Nederland.

]]>

This function will calculate the number of working days / weekdays (monday till friday) between two dates. It works the same as the datediff function but skips the weekends (saturday – sunday). I works easy, just add this little piece of code in your SELECT statement:

`SELECT dbo.DateDiffWorkDays('2012-01-07','2012-01-14')`

**Download this function below:**

The second method is better. This one uses my date dimension. I assume you also use one in your datamart I convert the datetime to an int to get proper results.

Please configure this function on your needs.

```
CREATE FUNCTION [dbo].[DateDiffWorkDays]
( @StartDate smalldatetime, @EndDate smalldatetime, @IncludeHolidays BIT = 0 )
RETURNS INT
AS
BEGIN
DECLARE @StartDateID INT
SET @StartDateID = CONVERT(VARCHAR,@StartDate,112)
DECLARE @EndDateID INT
SET @EndDateID = CONVERT(VARCHAR,@EndDate,112)
DECLARE @NrOfDays INT
SELECT @NrOfDays = COUNT(*)
FROM dbo.Datum AS DD
WHERE DD.DatumID BETWEEN @StartDateID AND @EndDateID
AND IsWerkDag= 1
AND ( @IncludeHolidays = 1
OR IsFeestDag_NL = 0)
RETURN ( @NrOfDays )
END
```

Het bericht Calculate DateDiff With Only WeekDays or Workdays verscheen eerst op SQLBlog Nederland.

]]>