Peter Larsson Blog

Patron Saint of Lost Yaks

Fast easter day function

CREATE FUNCTION dbo.fnGetEasterDate
(
    @Year SMALLINT
)
RETURNS DATE
AS
BEGIN
    RETURN (
                SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CONVERT(DATETIME, CAST(@Year AS CHAR(4)) + BaseDate, 112)) / 7 * 7, 6)
                FROM    (
                            SELECT CASE @Year % 19
                                        WHEN  0 THEN '0415'
                                        WHEN  1 THEN '0404'
                                        WHEN  2 THEN '0324'
                                        WHEN  3 THEN '0412'
                                        WHEN  4 THEN '0401'
                                        WHEN  5 THEN '0419'
                                        WHEN  6 THEN '0409'
                                        WHEN  7 THEN '0329'
                                        WHEN  8 THEN '0417'
                                        WHEN  9 THEN '0406'
                                        WHEN 10 THEN '0326'
                                        WHEN 11 THEN '0414'
                                        WHEN 12 THEN '0403'
                                        WHEN 13 THEN '0323'
                                        WHEN 14 THEN '0411'
                                        WHEN 15 THEN '0331'
                                        WHEN 16 THEN '0418'
                                        WHEN 17 THEN '0408'
                                        WHEN 18 THEN '0328'
                                        ELSE NULL
                                    END
                            WHERE   @Year BETWEEN 1900 AND 9999
                        ) AS d(BaseDate)
            )
END

Legacy Comments


iStan
2011-01-17
re: Fast easter day function
Hi,

Maybe its me, but the / 7 * 7 is a 0 operation, it could be left out.

ciao.

Peso
2011-01-17
re: Fast easter day function
You are completely right. It is you.

I am not sure, but it seems to me you are forgetting about integer division.
The first division by 7 ignores the decimal part in the result. Later, when multiplying with 7 you get a result which is the nearest lower value [than the original] that also is a tuple of 7.

//Peso