显示季度可用的月份名称

时间:2012-12-21 04:28:47

标签: sql sql-server sql-server-2008 tsql

我想显示Quarters中可用的月份名称。 像我们一年有四个季度1,2,3,4这样的手段。我有以下查询。

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

输出是:十月

但是我应该如何修改这个查询,以便我得到输出:

October
November
December

3 个答案:

答案 0 :(得分:0)

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) ) )

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

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)))