Last active
October 29, 2024 15:08
-
-
Save ianthrive/fbd985c155259905868b to your computer and use it in GitHub Desktop.
PostgreSQL functions to determine if a date is a leap year. Two different functions to demonstrate two different ways.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION is_leap_year(year integer) | |
RETURNS BOOLEAN AS $$ | |
SELECT ($1 % 4 = 0) AND (($1 % 100 <> 0) or ($1 % 400 = 0)) | |
$$ LANGUAGE sql IMMUTABLE STRICT; | |
CREATE OR REPLACE FUNCTION is_leap_year(date date) | |
RETURNS BOOLEAN AS $$ | |
SELECT DATE_PART('month', DATE_TRUNC('year', $1)+'1 months 28 days'::INTERVAL) = 2; | |
$$ LANGUAGE sql IMMUTABLE STRICT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment