Wednesday, December 13, 2017

SQL SERVER - 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)))

To get number of days in the current month:
SELECT DAY(DATEADD(DD,-1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)))

To get number of days in the next month:
SELECT DAY(DATEADD(DD,-1,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 2, 0)))

sOURCE : http://zarez.net/?p=2532

No comments: