簡體   English   中英

如何使用SQL獲取會計年度的所有月份的最后日期

[英]How to get all the month last date in the fiscal year using sql

我將輸入指定為'jun - may'

檢查當前年份和月份:

  1. 如果當前月份為9月(9),當前年份為2014,則會計年度設置為June 2014 - May 2015
  2. 如果當前月份為1月(1),當前年份為2014,則會計年度設置為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM