I want to have a function that calculates the fiscal year. The fiscal year must begin on the first Monday in March. Thank you! example:
CREATE FUNCTION dbo.fnc_FiscalYear( @AsOf DATETIME )
RETURNS INT
AS BEGIN
DECLARE @Answer INT
SET DATEFIRST 1
IF ( MONTH(@AsOf) < 3 )
or MONTH(@AsOf=3) and datename(weekday, @AsOf) = 'Monday' and datepart(day, @AsOf)>=1 and datepart(day, @AsOf)<=7;
SET @Answer = YEAR(@AsOf) - 1
ELSE SET @Answer = YEAR(@AsOf)
RETURN @Answer
END
GO
but it's not working
It looks as though there are a number of syntax errors with your script. Try this instead. I've removed the SETs and returned at the point of the if statements. Also, note the grouping of the if statements.