Sunday, 26 April 2009

How to know the number of days in a month in t-sql

Some times we can get requirement to know the number of days in a given month. For example, while calculating how many days remaining in a month or last day of month or any other scenarios.
Here is a small script that will give you the number of days for given month. Example purpose, i am retrieving 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