Replace in Date within a FOR

2

I want to return the last 12 months, the day should come as 01 and need month and year. ex:

2014-12-01
2014-11-01

NOTE: I placed yyyy-mm-dd only by a pattern of mine.

I'm doing this:

DECLARE @count INT
DECLARE @Data_Atual DATE
SET @count = 0
WHILE (@count < 12)
BEGIN
   SET @count = (@count + 1)    -- count++

   SET @Data_Atual = DATEADD(month, -@count, GETDATE()) --as 12 datas


   print @Data_Atual

END

The problem that today is returning 08:

2014-12-08
2014-11-08
2014-10-08
2014-09-08
2014-08-08
2014-07-08
2014-06-08
2014-05-08
2014-04-08
2014-03-08
2014-02-08
2014-01-08

And I want to do a replace on the day for 01, so does the form. but I need it to remain a DATE type because I'm going to use it in a select ...

    
asked by anonymous 08.01.2015 / 19:05

1 answer

3

While maintaining your algorithm, add this line that will be set to @Data_Atual the first day of the month of @Data_Atual itself:

SET @Data_Atual = DATEADD(month, DATEDIFF(month, 0, @Data_Atual), 0)
    
08.01.2015 / 19:11