简体   繁体   English

自当前日期起的财政年度

[英]Fiscal Year from Current Date

I'm using SSMS/SSRS2012 working on report to capture all hours worked in the current fiscal year, which for this purpose is Oct 1-Sep 30.我正在使用 SSMS/SSRS2012 处理报告来捕获当前财政年度的所有工作时间,为此目的是 10 月 1 日至 9 月 30 日。

I'm looking for a case statement that says the following: if the current month < 10 (october) then @FYStart = last year if the current month >= 10 then @FYStart = current year我正在寻找一个案例陈述,其中包含以下内容:如果当前月份 < 10(10 月)然后 @FYStart = 去年如果当前月份 >= 10 那么 @FYStart = 当前年份

When I query SELECT GETDATE() here is the format: 2020-06-16 15:24:57.637当我查询SELECT GETDATE()时,格式为: 2020-06-16 15:24:57.637

I have tried the following, but it only half works.我已经尝试了以下方法,但它只工作了一半。

SELECT CASE WHEN DATEPART(MONTH,(CAST(getdate() AS INT)))>09 THEN YEAR(CAST(getdate() AS INT)) ELSE DATEADD(YEAR,-1,(CAST(getdate() AS INT))) END

The result from this gives me 2019-06-17 00:00:00.000 which is a step in the right direction, but if I change the month to a month that has already passed,结果给了我2019-06-17 00:00:00.000这是朝着正确方向迈出的一步,但是如果我将月份更改为已经过去的月份,

SELECT CASE WHEN DATEPART(MONTH,(CAST(getdate() AS INT)))>03 THEN YEAR(CAST(getdate() AS INT)) ELSE DATEADD(YEAR,-1,(CAST(getdate() AS INT))) END

I get this result: 1905-07-14 00:00:00.000我得到这个结果: 1905-07-14 00:00:00.000

Something is obviously going wrong here but I'm not sure what exactly.这里显然出了问题,但我不确定到底是什么。 I'm thinking it's something with the data types but I'm not sure what to check/where to start.我认为这与数据类型有关,但我不确定要检查什么/从哪里开始。

So from what I gather, you are trying to isolate the year (as an integer) from today's date and store it in @FYStart .因此,根据我的收集,您正试图将年份(作为整数)与今天的日期隔离并将其存储在@FYStart中。 If today's date is before October, you want to assign it to last year, and if it's October or later, assign it to this year, correct?如果今天的日期在 10 月之前,你想把它分配给去年,如果是 10 月或更晚,把它分配给今年,对吗?

If so, try this:如果是这样,试试这个:

DECLARE @FYStart int
SET @FYStart = (
    SELECT CASE WHEN DATEPART(MONTH, GETDATE()) < 10
        THEN DATEPART(YEAR, DATEADD(YEAR, -1, GETDATE())) -- last year
    ELSE DATEPART(YEAR, GETDATE())  -- this year
    END
    )

Unless I'm overlooking something, is it not just something as simple as...除非我忽略了某些东西,否则它不只是像...

SELECT CASE WHEN MONTH(getdate()) <10
        THEN YEAR(getdate()) -1
        ELSE YEAR(getdate())
        END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM