Calculate Easter Day

Submitted by:Jhon Smith

Date added:03 May, 2012

Category:MySQL

MYSQL code example for calculation of Easter Day
This code taken from en.wikipedia.org

Tags: calculate day

Code Snippet:

DELIMITER //

DROP FUNCTION IF EXISTS f_easter
//
CREATE FUNCTION f_easter(
p_year YEAR
)
RETURNS VARCHAR(255)
DETERMINISTIC NO SQL
COMMENT 'Calculates Easter day for a given year.'
BEGIN
--
DECLARE a SMALLINT DEFAULT p_year % 19;
DECLARE b SMALLINT DEFAULT p_year DIV 100;
DECLARE c SMALLINT DEFAULT p_year % 100;
DECLARE d SMALLINT DEFAULT b DIV 4;
DECLARE e SMALLINT DEFAULT b % 4;
DECLARE f SMALLINT DEFAULT (b + 8) DIV 25;
DECLARE g SMALLINT DEFAULT (b - f + 1) DIV 3;
DECLARE h SMALLINT DEFAULT (19*a + b - d - g + 15) % 30;
DECLARE i SMALLINT DEFAULT c DIV 4;
DECLARE k SMALLINT DEFAULT c % 4;
DECLARE L SMALLINT DEFAULT (32 + 2*e + 2*i - h - k) % 7;
DECLARE m SMALLINT DEFAULT (a + 11*h + 22*L) DIV 451;
DECLARE v100 SMALLINT DEFAULT h + L - 7*m + 114;

RETURN STR_TO_DATE(
CONCAT(
p_year
, '-'
, v100 DIV 31
, '-'
, (v100 % 31) + 1
)
, '%Y-%c-%e'
);
END;
//

delimiter ;
 
 

Comments