Technical Article

Function to get number of days in month

,

Use this function to get number of days in the month of the supplied date value

CREATE FUNCTION [dbo].[GetDaysInMonth] ( @day DATETIME )
RETURNS INT
AS
BEGIN

RETURN CASE WHEN MONTH(@day) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
 WHEN MONTH(@day) IN (4, 6, 9, 11) THEN 30
 ELSE CASE WHEN (YEAR(@day) % 4 = 0 AND --Leap Year
 YEAR(@day) % 100 != 0) OR
 (YEAR(@day) % 400 = 0)
 THEN 29
 ELSE 28
 END
 END

END
GO

Rate

3.13 (8)

You rated this post out of 5. Change rating

Share

Share

Rate

3.13 (8)

You rated this post out of 5. Change rating