Monday, June 6, 2016

GetMonthListBetweenTwoDate in SQL Server

//MonthListBetweenTwoDate in SQL Server
CREATE PROCEDURE [dbo].[Get_MonthListBetweenTwoDate]
AS
BEGIN

DECLARE @StartDate  DATETIME,
        @EndDate    DATETIME;

SET @StartDate = '2013-05-31'      
SET @EndDate   = '2014-05-30';



SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate))[Month],DATENAME(YEAR, DATEADD(MONTH, x.number, @StartDate))[Year]
FROM    master.dbo.spt_values x
WHERE   x.type = 'P'      
AND     x.number <= DATEDIFF(MONTH, @StartDate, @EndDate)
end



No comments: