繁体   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