How to Get Number of Days in a Month in SQL Server

To get number of days in the previous month in SQL Server, use the following code:

SELECT DAY(DATEADD(DD,-1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))

number_of_days_previous_month

To get number of days in the current month:

SELECT DAY(DATEADD(DD,-1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)))

number_of_days_current_month

To get number of days in the next month:

SELECT DAY(DATEADD(DD,-1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 2, 0)))

number_of_days_next_month