SQllearner
SQllearner

Reputation: 41

Display the month names available in quarters

I want to display the month names available in the Quarters. Means like we have four quarters 1,2,3,4 in a year. I have following query.

select  datename(month,(DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), 0)) )

Output is :October

But How should I modify this query so that I should get the output as:

October
November
December

Upvotes: 3

Views: 2381

Answers (3)

Raj
Raj

Reputation: 10853

SELECT DATENAME(m,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)) AS MonthsInQuarter
UNION ALL
SELECT DATENAME(m,DATEADD(mm,1,DATEADD(qq,DATEDIFF(qq,0,getdate()),0))) 
UNION ALL
SELECT DATENAME(m,DATEADD(mm,2,DATEADD(qq,DATEDIFF(qq,0,getdate()),0)))

Upvotes: 0

rs.
rs.

Reputation: 27427

Try this

DECLARE @d datetime = Dateadd(mm,0,getdate())
-- set @d = Dateadd(mm,-3,getdate())

;With Dates
AS
(
SELECT DATEADD(qq,DATEDIFF(qq,0,@d),0) AS StartDt,DATEPART(qq,@d) AS Qr
UNION ALL
SELECT DATEADD(mm,1,StartDt),DATEPART(qq,DATEADD(mm,1,StartDt+1)-1)
FROM Dates
WHERE DATEPART(qq,DATEADD(mm,1,StartDt+1)-1)  = DATEPART(qq,@d)
)
SELECT 
datename(month,StartDt) Mnth, Qr,StartDt
FROM Dates

Upvotes: 0

Michael Fredrickson
Michael Fredrickson

Reputation: 37388

declare @quarter datetime
set @quarter = DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), 0)

select datename(month, @quarter)
union all
select datename(month, (DATEADD(month, 1, @quarter) ) )
union all
select datename(month, (DATEADD(month, 2, @quarter) ) )

Upvotes: 1

Related Questions