2014/12/08

SQL SERVER - Nombre de los meses entre intervalos de fechas

Como listar el nombre de los meses entre dos intervalos de tiempo?.
He aquí una manera de lograrlo.
 
DECLARE @StartDate DATETIME
       ,@EndDate DATETIME;
SELECT @StartDate = '20140301' -- March
      ,@EndDate = '20140901'; -- September
SELECT DATENAME(MONTH ,DATEADD(MONTH ,nos.monthnos ,@StartDate)) AS MonthName
FROM   (
           SELECT 1 monthnos UNION SELECT 2 UNION SELECT 3
           UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
           UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
           UNION SELECT 10 UNION SELECT 11 UNION SELECT 12
       ) nos
WHERE  nos.monthnos<= DATEDIFF(MONTH ,@StartDate ,@EndDate);

No hay comentarios.:

Publicar un comentario