Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have the below code which prints out the previous 12 months however it populates in one column in a tabe. How do I get these 12 months to be populated within a variable? Should I try using a while loop?
SQL
declare @date date = '03 December 2015'
;WITH T AS(
SELECT 
    DATEADD(month, -1, @date) AS Start,
    12 Cnt
    UNION ALL
SELECT 
     DATEADD(month, -1, Start),
     Cnt-1
FROM T
WHERE Cnt-1>=0
)

SELECT     case when  LEN(MONTH(Start)) = 1 then  substring( cast(YEAR(start) as varchar(6))  + right('0',1)+ cast(MONTH(Start) as varchar(6)),1,10)
else   substring( cast(YEAR(start) as varchar(6))  + cast(MONTH(Start) as varchar(6)),1,10) end [YearMonth]
FROM    t
Posted
Updated 3-Dec-15 0:56am
v2
Comments
John C Rayan 3-Dec-15 7:22am    
You are trying to populate these values into a table in a column right? Why do you need variable ? Just update/insert the table with the 12 values you got. What's the issue here?
Miss R 3-Dec-15 7:23am    
Because I want to pass these values within a tablename so I can get like 'xx_201504' and 'xx_201505' etc
ZurdoDev 3-Dec-15 7:42am    
You can't put them all into a single variable. What exactly are you trying to do?
Miss R 3-Dec-15 7:48am    
So I want the last 12 months on date input. I have the following but it does not calculate according to calendar yeas:
declare
--@enddate varchar(6),
@FirstTableMonth int =201412,
@Table_Name sysname,
@TableMonth int,
@end int,
@CurrentMonth int = 0,
@NextYearMonth int = 1,
@count int,
@enddate INT= 201601;


WHILE @CurrentMonth < 11
BEGIN

SELECT @TableMonth = CASE WHEN (@FirstTableMonth + @CurrentMonth) % 100 < 13 THEN
@FirstTableMonth + @CurrentMonth
ELSE
@FirstTableMonth + 100 - (@FirstTableMonth % 100) + @NextYearMonth
END,
@NextYearMonth = CASE WHEN (@FirstTableMonth + @CurrentMonth) % 100 < 13 THEN
@NextYearMonth
ELSE
@NextYearMonth - 1
END,


@Table_Name = 'xx_'+CAST(@TableMonth as nvarchar)+''

SET @CurrentMonth = CASE WHEN @TableMonth < @enddate THEN @CurrentMonth + 1 END

--PRINT @CurrentMonth;
PRINT @TableMonth;


END
Miss R 3-Dec-15 7:45am    
Heres the link: http://www.codeproject.com/Questions/1061494/SQL-Select-previous-months-based-on-date

I have made changes in the solution. Instead of providing input as integer I suggest to provide input in date like 01-Feb-2014' instead of 201402.

SQL
DECLARE @TBL_MONTH DATETIME, @NAME AS VARCHAR(20), @CurrentMonth INT, @TMP_MONTH DATETIME,
		@Table_Name VARCHAR(20)

SELECT @TBL_MONTH = '01-FEB-2014', @CurrentMonth = 12

WHILE @CurrentMonth > 0
BEGIN
	
	SET @CurrentMonth = @CurrentMonth - 1

	SET @TMP_MONTH = DATEADD(month, @CurrentMonth*-1,  @TBL_MONTH)

	SELECT @NAME = CONVERT(VARCHAR(6), @TMP_MONTH,  112)

	PRINT @NAME
	
	SELECT @Table_Name = 'xx_'+@NAME

	PRINT @Table_Name

END
 
Share this answer
 
v2
Comments
Miss R 3-Dec-15 7:52am    
No. The values should not store in a table, it should store as printed output
_Asif_ 3-Dec-15 7:56am    
You can do a loop over table, store the process_date in a variable @TABLENAME and then finally PRINT that variable as you are showing in ur comment :) though i can't comprehend what actually you are trying to acheive
Miss R 3-Dec-15 8:00am    
What I am trying to achieve is to get a list of the last 12 months. If i type 201402 then it should show me

201303
201304
201305
201306
201307
201308
201309
201310
201311
201312
201401
201402

The commented code above does not do this correctly as it brings up dates such as 201400 and 201399 which is incorrect.

Any idea how to do this?
_Asif_ 4-Dec-15 3:59am    
Have made changes to the solution. Try the new one
Miss R 3-Dec-15 8:23am    
Asif, here is a link: http://www.codeproject.com/Questions/1061569/SQL-get-last-months
It gives you what you wanted.

SQL
DECLARE @FirstTableMonth varchar(6) =  201402
DECLARE @inDate Date , @toDate Date

SET @inDate = Convert(DATE, @FirstTableMonth + '01')


SET @toDate =    DATEADD(month, -10,@inDate)

;with months (idate)
AS
(
    SELECT @inDate

    UNION ALL

    SELECT DATEADD(month,-1,isnull(idate,@inDate))
    from months
    where idate >= @toDate
)
select  CONVERT(CHAR(6), idate, 112)  from months
option (maxrecursion 0)



201402
201401
201312
201311
201310
201309
201308
201307
201306
201305
201304
201303
 
Share this answer
 
v2
Comments
Miss R 4-Dec-15 1:47am    
I need to pass each date within a variable. How do I do this? Because the tablename contains the date thus I need to pass it like such @tablename = xx_201401
John C Rayan 4-Dec-15 4:30am    
Again, you have to explain bit more clearer. What are you trying to do from these values by passing @tablename ?
SQL
declare @date date = '03 December 2015'
declare @list varchar(500)
SET @list = (SELECT   LEFT(CONVERT(VARCHAR,DATEADD(MM, -1*number, @date),112),6) + CHAR(10)
FROM    master.dbo.spt_values
WHERE TYPE = 'P' and number between 1 and 12
for xml path(''))
select @list
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900