Use this function to get number of days in the month of the supplied date value
2016-06-20
2,868 reads
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