Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Easter Calculation - SQL Server

RSS
Modified on Sun, Feb 23, 2020, 9:38 AM by Administrator Categorized as SQL Server

Overview

This article provide T-SQL (SQL Server) code to calculate the date of Easter for any given year.

Code

create function [dbo].[CalculateEaster](@year int)
returns date
as
/*--- Begin Testing ---*/
/*
declare @year int = 2020 -- easter = 4/12/2020
*/
/*--- End Testing ---*/
begin

declare 
     @century int = @year / 100 
    ,@remainder19 int = @year % 19

declare
     @centuryAdj int = (@century - 17) / 25

declare
     @yearAdj1 int = @century
                    - (@century / 4)
                    - ((@century - @centuryAdj) / 3)
                    + (19 * @remainder19)
                    + 15

set @yearAdj1 = @yearAdj1 % 30

set @yearAdj1 = @yearAdj1 
                - (@yearAdj1 / 28) *
                    (1 - (@yearAdj1 / 28) * (29 / (@yearAdj1 + 1)) * ((21 - @remainder19) / 11))

declare @yearAdj2 int = @year + @year / 4 + @yearAdj1 + 2 - @century + @century / 4

set @yearAdj2 = @yearAdj2 % 7

declare @yearAdjNet int = @yearAdj1 - @yearAdj2

declare @month int = 3 + (@yearAdjNet + 40) / 44

declare @day int = @yearAdjNet + 28 - (31 * (@month / 4))

declare @result date = datefromparts(@year, @month, @day)

return @result

--select result = @result

end

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.