months between two dates in sql server with starting and end date of each of them in sql server

前端 未结 6 1460
误落风尘
误落风尘 2020-12-20 00:07

i want to get months between two dates with their starting and end dates.Suppose if i enter startdate as \"2017-04-01\" and enddate as \"2017-07-31\", i want list of months

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 00:30

    If this is not just a one-off report, then I would create a calendar table, and use that to "group by". This will also let you do many other date related calculations. You can find one at simple calendar or one here at Stackoverflow

    Then your code could look like this:

    SELECT top 10000 * FROM dbo.calendar DD
    WHERE DD.TimeStampFrom>='2017-04-01' AND DD.TimeStampFrom <='2017-07-31'
    AND DAY(DD.TimeStampFrom)=1
    

提交回复
热议问题