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

前端 未结 6 1458
误落风尘
误落风尘 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条回答
  •  悲哀的现实
    2020-12-20 00:26

    Try this:

     DECLARE @Start DATE ='2017-04-01', 
            @End   DATE ='2017-07-31'
    
    
    SELECT *, Datename(mm, date), 
           Dateadd(mm, Datediff(mm, 0, date), 0) AS FirstDateOfMonth, 
           Dateadd (dd, -1, Dateadd(mm, Datediff(mm, 0, date) + 1, 0)) as 
          LastDateOfMonth
    FROM   dbo.TableName
    WHERE  Cast(date AS DATE) BETWEEN @Start AND @End 
    

提交回复
热议问题