在 SQL Server 中计算会计年度
如何根据 SQL Server 视图中的日期字段计算会计年度?
How would you calculate the fiscal year from a date field in a view in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何根据 SQL Server 视图中的日期字段计算会计年度?
How would you calculate the fiscal year from a date field in a view in SQL Server?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(22)
我建议您根据申请的财政年度使用用户定义的函数。
像这样使用它:
I suggest you use a User-Defined Function based on the Fiscal year of your application.
Use it like this:
这是澳大利亚财政年度开始日期代码,
它返回类似
'2012-07-01 00:00:00.000'
Here is Australian Financial year start date code
It returns like
'2012-07-01 00:00:00.000'
在这种情况下,财政年度从 7 月 1 日开始。这是最简单的解决方案。
In this case, Fiscal Year starts on 7/1. This is the simplest solution out there.
我扩展了 ChrisF 和 Conficker 发布的答案。
I've extended the answer posted by ChrisF and Conficker.
这种情况的最简单表达:
YEAR(DATEADD(month, 3, Date))
联邦财政年度
这可能不适用于美国以外的其他国家和地区,但您只需根据需要替换数字3即可。
Simplest expression for this case:
YEAR(DATEADD(month, 3, Date))
The Federal Fiscal Year
This may not apply to other countries and areas than the US, but you just have to replace the number 3 according to your needs.
我认为你不能,因为没有通用的财政日历。不同企业和国家的财政年度各不相同。
附录:您需要做的是拥有一个单独的数据库表,其中包含每个适用年份的会计开始日期和会计结束日期。使用该表中的数据计算给定特定日期的会计年度。
I don't think you can, because there is no universal fiscal calendar. Fiscal years vary between businesses and countries.
ADDENDUM: What you would need to do is have a separate DB table consisting of a fiscal start date, and a fiscal end date for each applicable year. Use the data in that table to calculate the fiscal year given a particular date.
您需要多个字段来执行此操作...
您应该检查会计年度的定义,因为它因公司而异
You would need more than a single field to do this...
You should check your definition of fiscal year as it varies from company to company
给定的
@FiscalYearStartMonth
是您的财政年度开始月份(数字)并且
@Date
是有问题的日期,请执行以下操作:您可以在函数中将其抽象化,或用作派生视图中的列
Given
@FiscalYearStartMonth
is your fiscal year start month (numeric)and
@Date
is the date in question, do the following:You can abstact this away in a function, or use as a column in a derived view
我刚刚意识到布雷特·文斯特拉的标记答案是错误的。 FY不应该这样计算吗?:
I just realized that the marked answer by Brett Veenstra is wrong. Shouldn't The FY should be calculated like this?:
基于 @csaba-toth 的上述答案,假设您的财政年度从该月的第一天开始
year(dateadd(month, (12 -
FyStartMonth+ 1),)
我的财政年度从 1 月 1 日开始,即第 7 个月,所以我的常量是 (12 - 7 + 1 =) 6.
测试用例(如2019 年 9 月 25 日):
返回:
我确实相信这是最简单、也许也是最容易理解的实现。
Building on the answer above by @csaba-toth, and assuming your fiscal year starts on the first day of the month
year(dateadd(month, (12 -
FyStartMonth+ 1), <date>)
My FY starts 1-July, the 7th month, so my constant is (12 - 7 + 1 =) 6.
Test cases (as of 25-Sep-2019):
Returns:
I do believe this is the simplest and perhaps most comprehensible implementation.
对于过去一年且开始日期为
oct 1st 10-1-2021
代码:
For one year in the past and a start date of
oct 1st 10-1-2021
Code:
这是英国的动态代码,
您可以根据不同的需求进行修改,
Here is the dynamic code for UK,
You can work around based on different needs,
会计年度开始:
会计年度结束:
如果需要,请将
getdate()
替换为您自己的日期Start of fiscal year:
End of Fiscal Year:
Replace
getdate()
with your own date if required这是我的版本,它返回财政年度为 FYyyyy - 财政年度从 7/1 开始
,即 6/1/2015 -> FY1415,2015 年 7 月 1 日 -> FY1516
字符串函数可以更好...
Here's my version which returns fiscal year as FYyyyy - fiscal year begins 7/1
i.e. 6/1/2015 -> FY1415, 7/1/2015 -> FY1516
String functions could be better...
如果您只想在查询中活动/当前会计年度,您可以使用类似以下内容:
您也可以这样使用:
获取当前会计年度的开始/结束日期。 (这是基于 10 月 31 日的年末,但显然可以根据需要进行调整。)然后可以在这样的查询中使用它,以便您的报告自动调整为活动会计年度:
If you just want the active/current Fiscal Year in a query, you can use something like this:
Which you can also use like this:
To get the start/end date of the current fiscal year. (This is based on a Oct. 31st year end, but can obviously be adjusted as desired.) This can then be used in a query like this so your report is automatically adjusted to the active fiscal year:
我只是不得不这样做,发现现有的答案很难理解,所以这里有一个针对英国的详细解释。
英国计算
说明:英国标准财政年度于 4 月 5 日结束,新财政年度于 4 月 6 日开始。然而,公司可以改变这一点,因此您的代码可能需要调整。
如果按原样运行,则会得到 2019 年 - 4 月 6 日是 2019 财政年度的第一个日期。
更改为 4 月 5 日,它将返回 2018 年。
解释
其工作方式是将您查看的日期向后移动,以便财政年度结束为正常年终 - 即 12 月 31 日 - 然后正常地从中获取年份。
在查询中使用内联很容易。如果您需要代码来处理不同的财务年度定义,您可以将
-5
和-3
放入参数中,然后简单地传入它们。日期偏移量为
-(财政年度第一天的月份 - 1)
月份偏移量为
-(财政年度第一天的月份 - 1)
注意:如果您的财政年度开始于首先,日期偏移量是
-(1 - 0)
- 或-0
或者当然是0
。在这种情况下,你可以完全消除这种情况,但我认为如果为了一致性而保留它会更清楚。
I just had to do this and found the existing answers hard to follow, so here's a detailed explanation for the UK.
UK Calculation
Note: The standard UK financial year ends on the 5th April, and a new one starts on the 6th April. Companies can however change that so your code may need to be tweaked.
If you run this as-is, you get 2019 - the 6th of April is the first date of the 2019 financial year.
Change to the 5th of April and it will return 2018.
Explanation
The way this works is to shift the date you are looking at back so the financial year end is the normal year end - i.e. 31st December - then take the year from that as normal.
It is easy to use inline in a query. If you needed the code to cope with different financial year definitions you could put the
-5
and-3
in parameters and simply pass them in.The day offset is
-(day of month of first day of financial year - 1)
The month offset is
-(month of first day of financial year - 1)
Note: If your financial year starts on the first, the day offset is
-(1 - 0)
- or-0
or of course0
.You could eliminate that condition entirely in that case but I think it's clearer if left in for consistency.
对于澳大利亚人来说更简单:)
(YEAR(DATEADD(Month,-((DATEPART(Month,[Date])+5) %12),[Date]))+) AS Financial_Year
More simple for Australians :)
(YEAR(DATEADD(Month,-((DATEPART(Month,[Date])+5) %12),[Date]))+) AS Financial_Year
简单的方法 -
DECLARE @DATE DATETIME = '2016/07/1'
-- 财政开始
SELECT CONVERT(DATETIME, (CAST(YEAR(@DATE) - IIF(MONTH(@DATE) > 6, 0, 1) AS VARCHAR) + '-7-1'))
-- 财政期末
选择 CONVERT(DATETIME, (CAST(YEAR(@DATE) + IIF(MONTH(@DATE) > 6, 1, 0) AS VARCHAR) + '-6-30'))
The simple way -
DECLARE @DATE DATETIME = '2016/07/1'
-- Fiscal Start
SELECT CONVERT(DATETIME, (CAST(YEAR(@DATE) - IIF(MONTH(@DATE) > 6, 0, 1) AS VARCHAR) + '-7-1'))
-- Fiscal End
SELECT CONVERT(DATETIME, (CAST(YEAR(@DATE) + IIF(MONTH(@DATE) > 6, 1, 0) AS VARCHAR) + '-6-30'))