首页 > 解决方案 > TSQL 显示下一个财政年度后的 4 月 1 日

问题描述

我在下面的查询中需要在下一个财政年度之前返回 7 月 1 日,然后在下一个财政年度之后返回到 4 月 1 日。即使在下一个财政年度之后,以下查询也会在 2019 年 7 月 1 日继续返回。

select  (case when getdate() < '2020-03-31' then '2019-07-01' 
when GETDATE() > '2020-03-31' then
dateadd(month, 3, dateadd(year, datepart(year, dateadd(month, -3, '2021-05-26')) - 1900, 0)) end)

任何帮助表示赞赏

标签: tsql

解决方案


这是因为在您when使用的条件下,getDate()它总是返回今天的日期。

尝试将其作为参数传递:

Declare @Date date
SET @Date = '2021-05-26'
select  (case when @Date < '2020-03-31' then '2019-07-01' 
when @Date > '2020-03-31' then
dateadd(month, 3, dateadd(year, datepart(year, dateadd(month, -3, @Date)) - 1900, 0)) end)

推荐阅读