How To Find All Months Of a Particular Quarter In SQL Server

The code is given below with which we get the list of months between a particular quarter.
 
Script
  1. DECLARE @AnyDate DATETIME  
  2. SET @AnyDate = GETDATE()  
  3.    
  4. DECLARE @StartDate  DATETIME,  
  5.         @EndDate    DATETIME;  
  6.  select @StartDate = DATEADD(q, DATEDIFF(q, 0, @AnyDate), 0)         
  7.   
  8.   select @EndDate   = DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, @AnyDate) + 1, 0));  
  9.   
  10.   
  11. SELECT  DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS MonthName  
  12. FROM    master.dbo.spt_values x  
  13. WHERE   x.type = 'P'          
  14. AND     x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);  
Output

 
 
Thanks.
Ebook Download
View all
Learn
View all
sourabhsomani.com