Skip to main content
Formatação do código
Fonte Link
Sam
  • 80,8mil
  • 22
  • 73
  • 127

DECLARE @dataInicio DATE DECLARE @dataFim DATE

BEGIN CREATE TABLE #calendario ( data DATE, yearmonth VARCHAR(9) )

DECLARE @dataInicio DATE 
DECLARE @dataFim DATE 

BEGIN 
    CREATE TABLE #calendario 
      ( 
         data      DATE, 
         yearmonth VARCHAR(9) 
      ) 

    SET @dataInicio = ( '1900-01-01' ) 
    SET @dataFim = ( Cast(Getdate() AS DATE) ) 

    WHILE @dataInicio <= @dataFim 
      BEGIN 
          INSERT INTO #calendario 
          VALUES      (@dataInicio, 
                       (SELECT Format(@dataInicio, 'MM-yyyy'))) 

          SET @dataInicio = Dateadd(day, 1, @dataInicio) 
      END 

    SELECT * 
    FROM   #calendario 
    ORDER  BY data DESC 
END 

END

DECLARE @dataInicio DATE DECLARE @dataFim DATE

BEGIN CREATE TABLE #calendario ( data DATE, yearmonth VARCHAR(9) )

SET @dataInicio = ( '1900-01-01' ) 
SET @dataFim = ( Cast(Getdate() AS DATE) ) 

WHILE @dataInicio <= @dataFim 
  BEGIN 
      INSERT INTO #calendario 
      VALUES      (@dataInicio, 
                   (SELECT Format(@dataInicio, 'MM-yyyy'))) 

      SET @dataInicio = Dateadd(day, 1, @dataInicio) 
  END 

SELECT * 
FROM   #calendario 
ORDER  BY data DESC 

END

DECLARE @dataInicio DATE 
DECLARE @dataFim DATE 

BEGIN 
    CREATE TABLE #calendario 
      ( 
         data      DATE, 
         yearmonth VARCHAR(9) 
      ) 

    SET @dataInicio = ( '1900-01-01' ) 
    SET @dataFim = ( Cast(Getdate() AS DATE) ) 

    WHILE @dataInicio <= @dataFim 
      BEGIN 
          INSERT INTO #calendario 
          VALUES      (@dataInicio, 
                       (SELECT Format(@dataInicio, 'MM-yyyy'))) 

          SET @dataInicio = Dateadd(day, 1, @dataInicio) 
      END 

    SELECT * 
    FROM   #calendario 
    ORDER  BY data DESC 
END 
Fonte Link

DECLARE @dataInicio DATE DECLARE @dataFim DATE

BEGIN CREATE TABLE #calendario ( data DATE, yearmonth VARCHAR(9) )

SET @dataInicio = ( '1900-01-01' ) 
SET @dataFim = ( Cast(Getdate() AS DATE) ) 

WHILE @dataInicio <= @dataFim 
  BEGIN 
      INSERT INTO #calendario 
      VALUES      (@dataInicio, 
                   (SELECT Format(@dataInicio, 'MM-yyyy'))) 

      SET @dataInicio = Dateadd(day, 1, @dataInicio) 
  END 

SELECT * 
FROM   #calendario 
ORDER  BY data DESC 

END