Question : Determining the number of working days during a specified month

I am trying to write the following function to determine the number of working days in a month, but I am having trouble with a syntax error.  Any suggestions:

CREATE FUNCTION udf_integer_Returns_WorkingDaysInSpecifiedMonth(@MONTH INT, @YEAR INT)
RETURNS INT
AS
BEGIN
DECLARE @FIRSTDAY DATETIME
DECLARE @LASTDAY DATETIME
DECLARE @MONTHDAYS INT
DECLARE @RESULT INT
SELECT @MONTHDAYS = CASE
      WHEN @MONTH = 1 THEN 31
      WHEN @MONTH = 2 THEN 28
      WHEN @MONTH = 3 THEN 31
      WHEN @MONTH = 4 THEN 30
      WHEN @MONTH = 5 THEN 31
      WHEN @MONTH = 6 THEN 30
      WHEN @MONTH = 7 THEN 31
      WHEN @MONTH = 8 THEN 31
      WHEN @MONTH = 9 THEN 30
      WHEN @MONTH = 10 THEN 31
      WHEN @MONTH = 11 THEN 30
      WHEN @MONTH = 12 THEN 31
END

SET @FIRSTDAY = CONVERT(DATETIME,(CONVERT(VARCHAR(2),@MONTH) + '/1/' + CONVERT(VARCHAR(4),@YEAR))
SET @LASTDAY = DATEADD(dd, @MONTHDAYS, @FIRSTDAY)
SET @RESULT = 0
IF @FIRSTDAY <= @LASTDAY
BEGIN
WHILE @FIRSTDAY <> @LASTDAY
BEGIN
SET @RESULT = @RESULT + CASE WHEN DATEPART(WEEKDAY,@FIRSTDAY) IN (1,7) THEN 0 ELSE 1 END
SET @FIRSTDAY = DATEADD(D,1,@FIRSTDAY)
END
END
RETURN @RESULT
END

Answer : Determining the number of working days during a specified month

Sorry, minor change.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
-- DROP FUNCTION GetWorkdays 
CREATE FUNCTION GetWorkdays (
  @month int,
  @year int 
  ) RETURNS INT AS BEGIN
  
	declare @result int  
	declare @seedDate datetime
	SET @seedDate = DATEADD(YEAR, @year - 1900, 0)

	SELECT @result = SUM(
	  CASE 
		 WHEN DATEPART(weekday,  DATEADD(d, number, @seedDate)) IN (1,7) 
		 THEN 0 
		 ELSE 1 
		 END
	  )
	  from master..spt_values
	  where type = 'p'
	  and YEAR(DATEADD(d, number, @seedDate)) = YEAR(@seedDate)
	  and MONTH(DATEADD(d, number, @seedDate)) = @month
	return @result
END
Random Solutions  
 
programming4us programming4us