Friday, October 2, 2015

How to determine the number of days in a month in SQL Server?

CREATE FUNCTION dbo.f__GetDaysInMonth(
                @Date DATE )
RETURNS TINYINT
AS
BEGIN
     DECLARE @Days TINYINT;
     SELECT @Days = DAY(DATEADD(month, DATEDIFF(MONTH, 0, @Date) + 1, -1));
 RETURN @Days
END;
GO
--Usage
SELECT dbo.f__GetDaysInMonth('11-Apr-2015')

No comments:

Post a Comment