Created
August 3, 2016 12:44
-
-
Save jonavon/662a3d7fd3cea682343e7a2b997c99df to your computer and use it in GitHub Desktop.
MySQL overlap function
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 overlap_interval(x INT,y INT,a INT,b INT) | |
RETURNS INTEGER DETERMINISTIC | |
BEGIN | |
DECLARE | |
overlap_amount INTEGER; | |
IF (((x <= a) AND (a < y)) OR ((x < b) AND (b <= y)) OR (a < x AND y < b)) THEN | |
IF (x < a) THEN | |
IF (y < b) THEN | |
SET overlap_amount = y - a; | |
ELSE | |
SET overlap_amount = b - a; | |
END IF; | |
ELSE | |
IF (y < b) THEN | |
SET overlap_amount = y - x; | |
ELSE | |
SET overlap_amount = b - x; | |
END IF; | |
END IF; | |
ELSE | |
SET overlap_amount = 0; | |
END IF; | |
RETURN overlap_amount; | |
END ;; | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment