[Solved] SQL query to get list of months start from current month


look at this query.
it’s just for demonstration,

DECLARE @YEAR INT
SET @YEAR = (SELECT YEAR(GETDATE()))
DECLARE @MONTH INT
SET @MONTH = (SELECT MONTH(GETDATE()))

DECLARE @DT TABLE(ID INT, MONTHNAME NVARCHAR(20))

INSERT INTO @DT SELECT NUMBER AS ID,
   DATENAME(MONTH, CAST(@YEAR*100+NUMBER AS VARCHAR) + '01')  AS MONTHNAME
FROM MASTER.DBO.SPT_VALUES   
WHERE TYPE = 'P' AND  
  NUMBER BETWEEN @MONTH AND 12

INSERT INTO @DT SELECT NUMBER AS ID,
   DATENAME(MONTH, CAST(@YEAR*100+NUMBER AS VARCHAR) + '01')  AS MONTHNAME

FROM MASTER.DBO.SPT_VALUES   
WHERE TYPE = 'P' AND  
  NUMBER BETWEEN 1 AND @MONTH -1

SELECT * FROM @DT 

9

solved SQL query to get list of months start from current month