Friday, November 28, 2014
SQL - Get Fiscal Year From Date
In SQL Reporting often we have the scenario where we need to do the reporting based on financial year. Fiscal year may vary from one organization to another, so feel free to update the function, Currently it set to Australian Financial Year.
SQL Function:
CREATE FUNCTION [dbo].[fnGetFiscalYear](
@CalenderDate DATETIME)
RETURNS NVARCHAR(10)
AS
BEGIN
DECLARE @FYYear INT
DECLARE @ResultVar NVARCHAR(10)
SET @ResultVar = ''
SELECT @FYYear = CASE WHEN MONTH(@CalenderDate) > 6 --New Fiscal Year stars on the fist day of July
THEN YEAR(@CalenderDate) + 1
ELSE YEAR(@CalenderDate)
END
SELECT @ResultVar = 'FY-' + RIGHT(@FYYear,2)
RETURN @ResultVar
END
Example:
SELECT GETDATE(), DBO.fnGetFiscalYear(GETDATE())
Result:
2014-11-28 13:52:21.603 FY-15
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment