0
我錯過了此處的邏輯,但我試圖從日曆月份開始財政月份。SQL Server:使用MONTH(DATEADD(M,6,@start))的日曆月中的會計月份結果爲13
代碼:
declare @start datetime,
@end datetime
set @start = '2012-01-01'
set @end = '2017-06-30'
;
with
calendar(date,y,q,m,d,dw,monthname,fy,fm) as
(
select @start,
year(@start),
datepart(qq,@start),
datepart(mm,@start),
datepart(dd,@start),
datepart(dw,@start),
datename(month, @start),
CASE WHEN DatePart(Month, @start) >= 6
THEN DatePart(Year, @start) + 1
ELSE DatePart(Year, @start)
END,
MONTH(DATEADD(M, 6, @start))
union all
select date + 1,
year(date + 1),
datepart(qq,date + 1),
datepart(mm,date + 1),
datepart(dd,date + 1),
datepart(dw,date + 1),
datename(month, date + 1),
CASE WHEN DatePart(Month, date) >= 6
THEN DatePart(Year, date) + 1
ELSE DatePart(Year, date)
END,
MONTH(DATEADD(M, 6, date)) + 1
from calendar where date + 1 < @end
)
select * from calendar
where d =1
option(maxrecursion 10000)
結果:
一切正常吧七月與13結束了?應該是1(7 + 6 = 13,所以1)
爲什麼你不重複在這些計算的遞歸部分中用'date + 1'替換'@ start'的模式? (我想,你沒有說過,'fm'意在成爲會計月) –