UFN_GLFISCALYEAR_COPYENDDATE
Used for Fiscal Year copy operation. Determines correct end date handling leap years.
Return
Return Type |
---|
datetime |
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@ENDDATE | datetime | IN | |
@YEARDIFFERENCE | int | IN |
Definition
Copy
CREATE function dbo.UFN_GLFISCALYEAR_COPYENDDATE(@ENDDATE datetime, @YEARDIFFERENCE int)
returns datetime
as
begin
declare @RETURNDATE datetime
set @RETURNDATE = dateadd(year, @YEARDIFFERENCE, @ENDDATE)
--T-SQL will correctly handle adding a year to 2/29 by setting it to 2/28. It doesn't
--correctly handle going in the other direction, however. But we only want to make this
--change if the original end date was not already 2/28 in the case of copying from a leap year.
if (month(@RETURNDATE) = 2) and (day(@RETURNDATE) = 28) and (@YEARDIFFERENCE % 4 <> 0)
begin
/*
To determine if this is a leap year, add a day to 2/28. If the month is still Feb
then it's a leap year and the day is now 2/29. If not, remove the day just added
to put it back at 2/28
*/
set @RETURNDATE = dateadd(day, 1, @RETURNDATE)
if month(@RETURNDATE) <> 2
set @RETURNDATE = dateadd(day, -1, @RETURNDATE)
end
return @RETURNDATE
end