Created
June 28, 2010 06:18
-
-
Save azisaka/455505 to your computer and use it in GitHub Desktop.
A SQL (MySQL) function that returns the number of the quadrimester based on the date
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
DELIMITER // | |
CREATE FUNCTION QUADRIMESTER (date_and_time DATETIME) | |
RETURNS INT(1) | |
BEGIN | |
DECLARE month_of_the_year INT(1); | |
SET month_of_the_year = MONTH(date_and_time); | |
IF month_of_the_year >= 1 AND month_of_the_year <= 4 THEN RETURN 1; | |
ELSE | |
IF month_of_the_year >= 5 AND month_of_the_year <= 8 THEN RETURN 2; | |
ELSE RETURN 3; | |
END IF; | |
END IF; | |
END // | |
DELIMITER ; | |
SELECT IF(QUADRIMESTER('2010-01-01') = 1, "PASS", "FAIL") "2010-01-01 == 1"; | |
SELECT IF(QUADRIMESTER('2010-04-30') = 1, "PASS", "FAIL") "2010-04-30 == 1"; | |
SELECT IF(QUADRIMESTER('2010-06-30') = 2, "PASS", "FAIL") "2010-06-30 == 2"; | |
SELECT IF(QUADRIMESTER('2010-08-31') = 2, "PASS", "FAIL") "2010-08-31 == 2"; | |
SELECT IF(QUADRIMESTER('2010-09-01') = 3, "PASS", "FAIL") "2010-09-01 == 3"; | |
SELECT IF(QUADRIMESTER('2010-12-31') = 3, "PASS", "FAIL") "2010-12-31 == 3"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment