Hi Check this Solution :
Declare @month int,
@year int,
@NoofDays int,
@ChkNoofDays int,
@ChkNoofDays_FEB int;
Declare @first DateTime,
@last DateTime,@last_WITH31 DateTime;
Set @month=5;
SET @NoofDays=31;
Set @year=2014;
Set @first=CAST(CAST(@year AS varchar) + '-' + CAST(@month AS varchar) + '-' + '1' AS DATETIME);
Set @ChkNoofDays=datediff(day,
dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month - 1, 0)),
dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month, 0))
)
Set @ChkNoofDays_FEB=datediff(day,
dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month - 1, 0)),
dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month, 0))
)
Set @last=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@first)+1,0));
select @last_WITH31= CASE WHEN @ChkNoofDays=28 THEN
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@first)+2,0))
ELSE
Case When @NoofDays=@ChkNoofDays then DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@first)+1,0))
ELSE DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@first)+2,0)) END
END
SELECT @first First_Day,@last last_Day,@last_WITH31 LASTDAY_WithResult;