[英]How to get all the month last date in the fiscal year using sql
我將輸入指定為'jun - may'
。
檢查當前年份和月份:
June 2014 - May 2015
。 Jun 2013 - May 2014
。 我需要查找會計年度內每個月的最后日期。
我找到了獲取一個月的最后一個日期的解決方案,但不知道如何根據邏輯將其擴展為查找范圍內的所有月份。
到目前為止,我的嘗試:
DECLARE @Month int
DECLARE @Year int
set @Month = 2
set @Year = 2004
select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-1900,0)))
你可以試試這個嗎
DECLARE @Month int =5
DECLARE @Year int = 2004
declare @i int = 0
if @Month between 1 and 5 set @i = 1 else set @i = 0
create table #temp(Monthend datetime)
set @Month = 6
while(@month<=12)
begin
insert into #temp
select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-@i-1900,0)))
set @month = @month+1
end
set @Month = 1
while(@month<6)
begin
insert into #temp
select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-@i-1899,0)))
set @month = @month+1
end
Select * from #temp
Drop table #temp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.