[英]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.