Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Meses entre duas datas

DECLARE @StartDate  DATETIME,
        @EndDate    DATETIME;

SELECT   @StartDate = '20110501'        
        ,@EndDate   = '20110801';


SELECT  DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS MonthName
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'        
AND     x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);

Resultados:
MonthName
------------------------------
May
June
July
August

(4 row(s) affected)