我想抓住两个日期之间每个季度的开始和结束日期
declare @pdtmParameter1 datetime = '30-Jun-2015'
declare @pdtmParameter2 datetime = '31-Mar-2017'
SELECT DISTINCT
Result should be
2015-03-31 00:00:00.000 2015-06-30 00:00:00.000
2015-07-01 00:00:00.000 2015-09-30 00:00:00.000
2015-10-01 00:00:00.000 2015-12-30 00:00:00.000
2015-12-31 00:00:00.000 2016-03-30 00:00:00.000
2016-03-31 00:00:00.000 2016-06-30 00:00:00.000
2016-07-01 00:00:00.000 2016-09-30 00:00:00.000
2016-10-01 00:00:00.000 2016-12-30 00:00:00.000
2016-12-31 00:00:00.000 2017-03-30 00:00:00.000
答案 0 :(得分:1)
declare @pdtmParameter1 datetime = '30-Jun-2015'
declare @pdtmParameter2 datetime = '31-Mar-2017'
SELECT DISTINCT DATEADD(Day,1,DATEADD(Month, (number-1)*3-3, @pdtmParameter1) ) AS QuarterStart,
DATEADD(Month, (number-1)*3, @pdtmParameter1) AS QuarterEnd
FROM (SELECT number
FROM (
VALUES (1), (2), (3), (4), (5),
(6), (7), (8)
) [1 to 8](number)
WHERE number BETWEEN 1 AND 8 ) getnumbers
答案 1 :(得分:0)
如果您正在寻找确切的季度开始和结束日期:
declare @pdtmParameter1 datetime = '30-Jun-2015'
declare @pdtmParameter2 datetime = '31-Mar-2017'
DECLARE @QUARTER_START INT
SET @QUARTER_START = DATEDIFF(q, 0, @pdtmParameter1)
SELECT * FROM
(
SELECT DATEADD(QUARTER, @QUARTER_START+QTR, 0) AS StartOfQuarter
,DATEADD(DD,-1,DATEADD(QUARTER, @QUARTER_START+QTR+1, 0)) AS EndOfQuarter
FROM (
SELECT QTR
FROM (
VALUES (0),(1), (2), (3), (4),(5), (6), (7), (8)
) [NUMBERS](QTR)
)T
)RESULT WHERE EndOfQuarter >= @pdtmParameter1 and EndOfQuarter <= @pdtmParameter2