Function to get Number of Days in a month
This function will help to get the Number of days in a given month. The function returns the Number of days from the date given as input. The advantage of the function is that it returns the days of a month for all years(including Leap years)
The below Function can be created in SQL Server to enable the developers to get the Number of Days of the given Month.
This function can be used in any StoredProcedures or Queries where there is a need to get Number of Days in a month.
Here is the code
CREATE FUNCTION [dbo].[ufn_GetNoOfDaysInMonth] ( @uDate DATETIME )
RETURNS INT
AS
BEGIN
RETURN CASE WHEN MONTH(@uDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@upDate) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(@uDate) % 4 = 0 AND
YEAR(@uDate) % 100 != 0) OR
(YEAR(@uDate) % 400 = 0)
THEN 29
ELSE 28
END
END
END
GO
Here the input to the Function is DateTime.
If you pass the DateTime to the function it returns the Number of Days in the month of the DateTime given as input.
This function can be used in common for bulk datas.
Hope this helps!!!
Regards,
S.Rajeswari
Steadfast technology