CREATE FUNCTION dbo.DaysInMonth  (@year int, @month int)
RETURNS int
AS
BEGIN
  declare @bDate varchar(8)

  if @month = 12 
  begin
    set @year = @year + 1
    set @month = 1
  end
  else
    set @month = @month + 1

  if @month < 10 
    set @bDate = convert(varchar(4), @year) + '0' + convert(varchar(4), @month) + '01' 
  else
    set @bDate = convert(varchar(4), @year) + convert(varchar(4), @month) + '01' 

  return day(convert(datetime, convert(int, convert(datetime, @bDate, 112)-1)))
end

