Ik heb zelf onderstaande geschreven (is trouwens makelijker te doen m.b.v. modulo (%))
CREATE FUNCTION aantalWerkdagen (@Datumbegin datetime, @Datumeind datetime, @tempCurrent datetime)
RETURNS int
AS
BEGIN
DECLARE @tempDatumB datetime
DECLARE @tempDatumE datetime
IF Datepart(yyyy, @Datumbegin) < (Datepart(yyyy, @tempCurrent))
AND Datepart(yyyy, @Datumeind) >(Datepart(yyyy, @tempCurrent))
AND @Datumbegin < @Datumeind
BEGIN
SET @tempDatumB = '01-01-' + CAST(Datepart(yyyy, @tempCurrent) AS CHAR(4))
SET @tempDatumE = '12-31-' + CAST(Datepart(yyyy, @tempCurrent) AS CHAR(4))
END
ELSE
BEGIN
IF Datepart(yyyy, @Datumbegin) < Datepart(yyyy, @tempCurrent)
AND Datepart(yyyy, @Datumeind) = Datepart(yyyy, @tempCurrent)
BEGIN
SET @tempDatumB = '01-01-' + CAST(Datepart(yyyy, @tempCurrent) AS CHAR(4))
SET @tempDatumE = @Datumeind
END
ELSE
BEGIN
IF Datepart(yyyy, @Datumbegin) = Datepart(yyyy, @tempCurrent)
AND Datepart(yyyy, @Datumeind) > Datepart(yyyy, @tempCurrent)
BEGIN
SET @tempDatumB = @Datumbegin
SET @tempDatumE = '12-31-' + CAST(Datepart(yyyy, @tempCurrent) AS CHAR(4))
END
ELSE
BEGIN
SET @tempDatumB = @Datumbegin
SET @tempDatumE = @Datumeind
END
END
END
DECLARE @wDagen int
SET @wDagen = 0
WHILE @tempDatumB <= @tempDatumE
BEGIN
IF DATEPART(dw, @tempDatumB) BETWEEN 1 AND 5
BEGIN
SET @wDagen = @wDagen + 1
SET @tempDatumB = @tempDatumB + 1
END
ELSE
BEGIN
SET @tempDatumB = @tempDatumB + 1
END
CONTINUE
END
RETURN @wDagen
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO