2012年3月9日星期五

Fiscal or Calendar Year Function error

Hello,
I am trying to create a function that returns the first day of the fiscal or
calendar year depending on the parameter supplied. However, I get the
error: Error 443: Invalid use of the 'getdate' within a function.
Any help would be appreciated.
Thanks in advance, sck10
CREATE FUNCTION dbo.fn_GetCalendarFiscalYear (@.YearType varchar(100))
RETURNS varchar(255)
AS
BEGIN
Declare @.strCalendarFiscal varchar(255)
SELECT @.strCalendarFiscal =
CASE
WHEN @.YearType = 'calendar' Then '1/1/' + Convert(varchar(255),
year(getdate()))
WHEN @.YearType = 'fiscal' Then
CASE
WHEN month(getdate()) BETWEEN 10 AND 12 THEN
'10/1/' + Convert(varchar(255), year(getdate()))
ELSE
'10/1/' + Convert(varchar(255), year(getdate()) - 1)
END
ELSE 'Pick "calendar" or "fiscal"'
END AS 'strFiscalYearStart'
RETURN @.strCalendarFiscal
Hi,
Welcome to use MSDN Managed Newsgroup!
I am so sorry that you can not put a function that returns a varible
result in a user defined function. Check MVP Aaron Bertrand's article for
this issue
How do I use GETDATE() within a User-Defined Function (UDF)?
http://www.aspfaq.com/2439
Thank you for your patience and cooperation. If you have any questions or
concerns, don't hesitate to let me know. We are always here to be of
assistance!
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no rights.

没有评论:

发表评论