3

我想显示季度中可用的月份名称。意味着我们一年有四个季度 1,2,3,4。我有以下查询。

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

输出是:十月

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

October
November
December
4

3 回答 3

1
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) ) )
于 2012-12-21T04:43:02.603 回答
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
于 2012-12-21T04:44:00.077 回答
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)))
于 2012-12-21T04:58:15.007 回答