How would you calculate the fiscal year from a date field in a view in SQL Server?
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
I suggest you use a User-Defined Function based on the Fiscal year of your application.
CREATE FUNCTION dbo.fnc_FiscalYear(
@AsOf DATETIME
)
RETURNS INT
AS
BEGIN
DECLARE @Answer INT
-- You define what you want here (September being your changeover month)
IF ( MONTH(@AsOf) < 9 )
SET @Answer = YEAR(@AsOf) - 1
ELSE
SET @Answer = YEAR(@AsOf)
RETURN @Answer
END
GO
Use it like this:
SELECT dbo.fnc_FiscalYear('9/1/2009')
SELECT dbo.fnc_FiscalYear('8/31/2009')
I totally agree with alex, I've been involved in development and implementation of various ERPs, local (ME) and global. having a Fiscal year table and Periods as well is the way to go.