Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>try:</p> <pre><code>DECLARE @YourTable table (YourDate datetime, value int) insert into @YourTable VALUES ('2011-7-12',5) ;WITH AllNumbers AS ( SELECT 0 AS Number UNION ALL SELECT Number+1 FROM AllNumbers WHERE Number&lt;4 ) SELECT dateadd(month,number,DATEADD(month,DATEDIFF(month,0,YourDate),0)) FROM @YourTable y INNER JOIN AllNumbers a ON 1=1 </code></pre> <p>output:</p> <pre><code>----------------------- 2011-07-01 00:00:00.000 2011-08-01 00:00:00.000 2011-09-01 00:00:00.000 2011-10-01 00:00:00.000 2011-11-01 00:00:00.000 (5 row(s) affected) </code></pre> <p>it works with multiple rows in the table, see here:</p> <pre><code>DECLARE @YourTable table (YourDate datetime, ValueOf int) insert into @YourTable VALUES ('2011-7-12',5) insert into @YourTable VALUES ('2012-4-24',6) ;WITH AllNumbers AS ( SELECT 0 AS Number UNION ALL SELECT Number+1 FROM AllNumbers WHERE Number&lt;4 ) SELECT y.ValueOf ,dateadd(month,number,DATEADD(month,DATEDIFF(month,0,y.YourDate),0)) FROM @YourTable y INNER JOIN AllNumbers a ON 1=1 ORDER BY 1,2 </code></pre> <p>OUTPUT:</p> <pre><code>ValueOf ----------- ----------------------- 5 2011-07-01 00:00:00.000 5 2011-08-01 00:00:00.000 5 2011-09-01 00:00:00.000 5 2011-10-01 00:00:00.000 5 2011-11-01 00:00:00.000 6 2012-04-01 00:00:00.000 6 2012-05-01 00:00:00.000 6 2012-06-01 00:00:00.000 6 2012-07-01 00:00:00.000 6 2012-08-01 00:00:00.000 (10 row(s) affected) </code></pre> <p>Also, I don't have SQL Server 2008 available, so I used datetime, if you have 2008, you can use <code>DATE</code> datatype and you don't have to floor the datetime, so use this line:</p> <pre><code>dateadd(month,number,y.YourDate) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload