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

If the post helped you, please share it:
Pin It

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

  • Arvind Kumar

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

    this will return days of current Month, you can get the same for any month you want by replacing Month(getdate()) with month number..

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">