7

I need a select to return Month and year Within a specified date range where I would input the start year and month and the select would return month and year from the date I input till today.

I know I can do this in a loop but I was wondering if it is possible to do this in a series selects?

Year  Month
----  -----
2010  1
2010  2
2010  3
2010  4
2010  5
2010  6
2010  7

and so on.

0

8 Answers 8

16

Gosh folks... using a "counting recursive CTE" or "rCTE" is as bad or worse than using a loop. Please see the following article for why I say that.

http://www.sqlservercentral.com/articles/T-SQL/74118/

Here's one way to do it without any RBAR including the "hidden RBAR" of a counting rCTE.

--===== Declare and preset some obviously named variables
DECLARE @StartDate DATETIME,
        @EndDate   DATETIME
;
 SELECT @StartDate = '2010-01-14', --We'll get the month for both of these 
        @EndDate   = '2020-12-05'  --dates and everything in between
;
WITH
cteDates AS
(--==== Creates a "Tally Table" structure for months to add to start date
     -- calulated by the difference in months between the start and end date.
     -- Then adds those numbers to the start of the month of the start date.
 SELECT TOP (DATEDIFF(mm,@StartDate,@EndDate) + 1)
        MonthDate = DATEADD(mm,DATEDIFF(mm,0,@StartDate) 
                  + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1),0)
   FROM sys.all_columns ac1
  CROSS JOIN sys.all_columns ac2
)
--===== Slice each "whole month" date into the desired display values.
 SELECT [Year]  = YEAR(MonthDate),
        [Month] = MONTH(MonthDate) 
   FROM cteDates
;
2
  • I like your method of doing it, but in the end isn't there still RBAR by calling the YEAR() and MONTH() functions on the MonthDate column?
    – Matt
    Sep 21, 2012 at 2:52
  • 1
    Behind the scenes, everything in T-SQL is actually based on a loop of one form or another so it all qualifies as "RBR". Since I'm the guy that coined the phrase, here's what "RBAR" has come to mean... RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row". The term has come to mean any code method that requires more resources or causes longer runs because of its row-by-row nature even if the code is actually "set based" code. Since most of the intrinsic functions of SQL Server operate at machine language speeds, I'd have to say, "No... this isn't RBAR". ;-)
    – Jeff Moden
    May 5, 2013 at 0:42
2

I know this is an old question, but I'm mildly horrified at the complexity of some of the answers. Using a CTE is definitely the simplest way to go for selecting these values:

WITH months(dt) AS 
   (SELECT getdate() dt 
    UNION ALL
    SELECT dateadd(month, -1, dt)
    FROM months)
SELECT 
top (datediff(month, '2017-07-01' /* start date */, getdate()) + 1) 
YEAR(months.dt) yr, MONTH(months.dt) mnth
FROM months
OPTION (maxrecursion 0);

Just slap in whichever start date you'd like in place of the '2017-07-01' above and you're good to go with an efficient and easily-integrated solution.


Edit: Jeff Moden's answer quite effectively advocates against using rCTEs. However, in this case it appears to be a case of premature optimization - we're talking about 10's of records in all likelihood, and even if you span back to 1900 from today, it's still a minuscule hit. Using rCTEs to achieve code maintainability seems to be worth the trade if the expected result set is small.

4
  • Why not practice doing it the right way all the time so that if you end up with unexpected growth or the code ends up being called thousands of times per hour, you don't have to find it and fix it.
    – Jeff Moden
    Aug 27, 2020 at 2:04
  • @JeffModen While I generally agree that preparing for unexpected growth is a good thing, there are a few things that come into play with the procedure approach which may make it a non-starter (beyond complexity). Specifically, if the programmer only has query access to a database, they still need a solution, and they can be empowered to evaluate whether their use case is likely to suffer performance implications in the future. For instance, in my case, the range is only going to grow by one record per month, so gradual performance loss is hardly a concern.
    – bsplosion
    Aug 31, 2020 at 14:58
  • You say that you're "mildly horrified by complexity" but, if you remove all comments from my code, it's a line shorter than yours. ;-) I'll also say that using such excuses for not doing it the right way, especially since it's so easy to do it the right way, is how "Death by a thousand cuts" creeps into a database and then everyone blames SQL Server for slow code. If it's worth doing at all, it's worth doing right and it takes no more effort to do so. Incremental rCTEs are never the right way.
    – Jeff Moden
    Sep 1, 2020 at 17:07
  • Fair enough, and it's possible to refactor your approach to avoid having any T-SQL implications by not using binds. I'm not usually a fan of using sys.all_columns as a generator since that feels hacky, but I suppose it's at no risk of running out of rows on a normal DB.
    – bsplosion
    Sep 2, 2020 at 13:33
2

You can use something like this: Link

To generate the equivalent of a numbers table using date ranges.

But could you please clarify your inputs and outputs?

Do you want to input a start date, for example, '2010-5-1' and end date, for example, '2010-8-1' and have it return every month between the two? Do you want to include the start month and end month, or exclude them?

Here's some code that I wrote that will quickly generate an inclusive result of every month between two dates.

--Inputs here:
DECLARE @StartDate datetime;
DECLARE @EndDate datetime;
SET @StartDate = '2010-1-5 5:00PM';
SET @EndDate = GETDATE();

--Procedure here:
  WITH RecursiveRowGenerator (Row#, Iteration) AS (
       SELECT 1, 1
        UNION ALL
       SELECT Row# + Iteration, Iteration * 2
         FROM RecursiveRowGenerator
        WHERE Iteration * 2 < CEILING(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1))
        UNION ALL
       SELECT Row# + (Iteration * 2), Iteration * 2
         FROM RecursiveRowGenerator
        WHERE Iteration * 2 < CEILING(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1))
     )
     , SqrtNRows AS (
       SELECT *
         FROM RecursiveRowGenerator
        UNION ALL
       SELECT 0, 0
     )
SELECT TOP(DATEDIFF(MONTH, @StartDate, @EndDate)+1) 
       DATEADD(month, DATEDIFF(month, 0, @StartDate) + A.Row# * POWER(2,CEILING(LOG(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1))/LOG(2))) + B.Row#, 0)  Row#
  FROM SqrtNRows A, SqrtNRows B
 ORDER BY A.Row#, B.Row#;
0
1

Code below generates the values for the range between 21 Jul 2013 and 15 Jan 2014. I usually use it in SSRS reports for generating lookup values for the Month parameter.

declare
    @from date = '20130721',
    @to date = '20140115';

with m as (
select * from (values ('Jan', '01'), ('Feb', '02'),('Mar', '03'),('Apr', '04'),('May', '05'),('Jun', '06'),('Jul', '07'),('Aug', '08'),('Sep', '09'),('Oct', '10'),('Nov', '11'),('Dec', '12')) as t(v, c)),

y as (select cast(YEAR(getdate()) as nvarchar(4)) [v] union all select cast(YEAR(getdate())-1 as nvarchar(4)))

select m.v + ' ' + y.v [value_field], y.v + m.c [label_field]
from m
cross join y
where y.v + m.c between left(convert(nvarchar, @from, 112),6) and left(convert(nvarchar, @to, 112),6)
order by y.v + m.c desc

Results:

value_field     label_field
---------------------------
Jan 2014        201401
Dec 2013        201312
Nov 2013        201311
Oct 2013        201310
Sep 2013        201309
Aug 2013        201308
Jul 2013        201307
0

you can do the following

SELECT DISTINCT YEAR(myDate) as [Year], MONTH(myDate) as [Month]
FROM myTable
WHERE <<appropriate criteria>>
ORDER BY [Year], [Month]
1
  • how would you use the "appropriate criteria" can you show example...like all dates from 2009-12 to today Nov 15, 2010 at 4:13
0

---Here is a version that gets the month end dates typically used for accounting purposes

DECLARE @StartDate datetime;
 DECLARE @EndDate datetime; 
 SET @StartDate = '2010-1-1'; 
 SET @EndDate = '2020-12-31';  
 --Procedure here:   





 WITH RecursiveRowGenerator (Row#, Iteration)                             
 AS (        SELECT 1, 1         
 UNION ALL        
 SELECT Row# + Iteration, Iteration * 2         
  FROM RecursiveRowGenerator         
  WHERE Iteration * 2 < CEILING(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1)) 
  UNION ALL        SELECT Row# + (Iteration * 2), Iteration * 2 
           FROM RecursiveRowGenerator         
           WHERE Iteration * 2 < CEILING(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1))      )  
               , SqrtNRows AS (        SELECT *          FROM RecursiveRowGenerator         
 UNION ALL        SELECT 0, 0      ) 
 SELECT TOP(DATEDIFF(MONTH, @StartDate, @EndDate)+1)         
           DateAdd(d,-1,DateAdd(m,1, DATEADD(month, DATEDIFF(month, 0, @StartDate) + A.Row# * POWER(2,CEILING(LOG(SQRT(DATEDIFF(MONTH, @StartDate, @EndDate)+1))/LOG(2))) + B.Row#, 0)  ))
Row#   FROM SqrtNRows A, SqrtNRows B  ORDER BY A.Row#, B.Row#; 
0
DECLARE @Date1 DATE
DECLARE @Date2 DATE

SET @Date1 = '20130401'
SET @Date2 = DATEADD(MONTH, 83, @Date1)

SELECT DATENAME(MONTH, @Date1) "Month", MONTH(@Date1) "Month Number", YEAR(@Date1) "Year"
INTO #Month

WHILE (@Date1 < @Date2)
BEGIN 
    SET @Date1 = DATEADD(MONTH, 1, @Date1)
    INSERT INTO #Month
    SELECT DATENAME(MONTH, @Date1) "Month", MONTH(@Date1) "Month Number", YEAR(@Date1) "Year"
END

SELECT * FROM #Month 
ORDER BY [Year], [Month Number]

DROP TABLE #Month
-1
declare @date1 datetime, 
    @date2 datetime, 
    @date  datetime, 
    @month integer, 
    @nm_bulan varchar(20) 

create table #month_tmp 
    ( bulan integer null, keterangan varchar(20) null ) 

select @date1 = '2000-01-01', 
       @date2 = '2000-12-31' 

select @month = month(@date1) 

while (@month < 13) 
Begin 
    IF @month = 1 
    Begin 
       SELECT @date  = CAST( CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,0,@date1))-1),DATEADD(mm,0,@date1)),111) + ' 00:00:00' as DATETIME ) 
    End
    ELSE
    Begin
       SELECT @date  = CAST( CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,@month -1,@date1))-1),DATEADD(mm,@month -1,@date1)),111) + ' 00:00:00' as DATETIME ) 
    End
    select @nm_bulan = DATENAME(MM, @date)

    insert into #month_tmp
    select @month as nilai, @nm_bulan as nama 

    select @month = @month + 1
End 

select * from #month_tmp 
drop table #month_tmp 
go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.