In this blog, we are creating a function that will return the number of days for a given month. Below is the code snippet for the function that is used to retrieve the number of days.
  1. CREATE FUNCTION Func_GetDate(@Date_ [date])
  2. RETURNS [int]
  3. BEGIN
  4. DECLARE @Date [datetime];
  5. SET @Date=@Date_;
  6. DECLARE @Day int;
  7. SET @Day=Day(@Date);
  8. DECLARE @new_Date [datetime];
  9. SET @new_Date=dateadd(day,-@day+1,@Date);
  10. SET @Date=dateadd(day,-1,dateadd(month,1,@new_Date));
  11. return datediff(Day,@new_Date,@Date)+1;
  12. END
Example