我将输入指定为'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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句