Friday, August 26, 2011

How to Know the Number of Days in a Month in T-SQL

Sometimes we may need to know the number of days in a given month. For example, in calculating the number of days in a month or last day of the month or any other scenario.

Here is a small script that will give you the number of days for a month. For example, I'll pick month from the current date.

declare @numberOfDaysInMonth int;

set @numberOfDaysInMonth = DAY(DATEADD (m, 1, DATEADD (d, 1 - DAY(getdate()), getdate())) - 1);

No comments:

Post a Comment