【问题标题】:How to get all the month last date in the fiscal year using sql如何使用sql获取财政年度中所有月份的最后日期
【发布时间】:2014-11-02 11:16:39
【问题描述】:

我将输入指定为'jun - may'

检查当前年份和月份:

  1. 如果当前月份为 SEPTEMBER (9),当前年份为 2014 年,则会计年度设置为 June 2014 - May 2015
  2. 如果当前月份为 JANUARY (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))) 

【问题讨论】:

  • 因此,如果当前月份介于 June to December 之间,例如 2014,则会计年度为 June 2014 to May 2015。但是,如果当前月份在 January to May 之间,再次说 2014,那么会计年度是 June 2013 to May 2014?对吗?
  • Xcatly 你是礼节!!!!
  • 那么,顺便说一下,输出应该是什么样子的呢?我应该将提取的范围与什么值进行比较?假设我将 2014 年 6 月到 2015 年 5 月与哪个值(或字段?)进行比较?还是我需要将 2014 年 6 月、2014 年 7 月、2014 年 8 月到 2015 年 5 月的值范围放在一个表格中?
  • 是的,埃德珀。我从下面的答案中得到了解决方案,感谢您花费宝贵的时间。非常感谢:)

标签: sql date


【解决方案1】:

你可以试试这个

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

【讨论】:

  • 谢谢传奇 :) 我不能给 +10,所以我给了 +1
猜你喜欢
  • 2015-07-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2021-12-26
相关资源
最近更新 更多