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

Even Mien picture Even Mien · Mar 27, 2009 · Viewed 242.9k times · Source

I need to determine the number of days in a month for a given date in SQL Server.

Is there a built-in function? If not, what should I use as the user-defined function?

Answer

Mikael Eriksson picture Mikael Eriksson · Feb 2, 2013

In SQL Server 2012 you can use EOMONTH (Transact-SQL) to get the last day of the month and then you can use DAY (Transact-SQL) to get the number of days in the month.

DECLARE @ADate DATETIME

SET @ADate = GETDATE()

SELECT DAY(EOMONTH(@ADate)) AS DaysInMonth