Get number of days in month in sql

Get number of days in month in sql :

Earlier we have gone through the way we can get the fist day and last day of the week in sql.

You can refer the post at : Get first and last day of week in sql

In this post we will find number of days in month using a date passed in.  The below query will return the number of days in month based on the date to be passed. Modify the query as per your need to make it suitable for your requirement.

DECLARE @DATE DATETIME
SET @DATE = '2017-09-21'

SELECT DATEDIFF(DAY, DATEADD(DAY, 1-DAY(@DATE), @DATE),
              DATEADD(MONTH, 1, DATEADD(DAY, 1-DAY(@DATE), @DATE)))

 

Leave a Reply