title: SQL query to get list of month between two dates
body: <p>I need a custom output of dates between two dates.Let me explain What exactly I need.</p>
  
  <p>Lets say I have two dates <code>'2016-01-01'</code> and <code>'2016-12-31'</code> <code>yyyy-mm-dd</code> Now I want to display list of months/date between these two dates but in order of 3 months,6,9,12,18,24 etc</p>
  
  <p>E.g. </p>
  
  <p>E.g. If I enter start and end date as <code>'2016-01-01'</code> and <code>'2016-12-31'</code> then output should display till 12 months as difference between start and end is 12.</p>
  
  <p>So output should be </p>
  
  <pre><code>3 Months    1/1/2016    4/1/2016
  6 Months    1/1/2016    7/1/2016
  9 Months    1/1/2016    10/1/2016
  12 Months   1/1/2016    1/1/2017
  </code></pre>
  
  <p>If start and end date are <code>'2016-01-01'</code> and <code>'2016-02-15'</code> then output should display 3 months as end date comes within 3</p>
  
  <p>So output should be</p>
  
  <pre><code>3 Months    1/1/2016    4/1/2016
  </code></pre>
  
  <p>sql is as below</p>
  
  <p>declare @startdate date = '2016-01-01', @endDate DATE = '2016-12-31'</p>
  
  <pre><code>declare @mindate date, @maxdate date
      SET @mindate = @startdate;
      SET @maxdate = @endDate
  
  
  
          INSERT INTO @ReportMonths(ReportMonth,MinMOnth,MaxMonth) values
      ( '3 Months',@mindate,dateadd(month,3,@mindate)),
      ( '6 Months',@mindate,dateadd(month,6,@mindate)),
      ( '9 Months',@mindate,dateadd(month,9,@mindate)),
      ( '12 Months',@mindate,dateadd(month,12,@mindate)),
      ( '18 Months',@mindate,dateadd(month,18,@mindate)),
      ( '24 Months',@mindate,dateadd(month,24,@mindate)),
      ( '36 Months',@mindate,dateadd(month,36,@mindate)),
      ( '48 Months',@mindate,dateadd(month,48,@mindate)),
      ( '60 Months',@mindate,dateadd(month,60,@mindate)),
      ( '72 Months',@mindate,dateadd(month,72,@mindate))
  </code></pre>
  
  <p>issue is how do i delete rest of rows when end date doesn't comes in Maxdate column</p>