Skip to content

Instantly share code, notes, and snippets.

@euclid1990
Created November 11, 2021 04:57
Show Gist options
  • Save euclid1990/9a5c5d16eeeefdd7d52986e2d786dd2a to your computer and use it in GitHub Desktop.
Save euclid1990/9a5c5d16eeeefdd7d52986e2d786dd2a to your computer and use it in GitHub Desktop.
Consider NULL as MAX in SQL
DROP FUNCTION IF EXISTS fconsider_null_as_max;
DELIMITER //
CREATE FUNCTION fconsider_null_as_max (originalYear INT)
RETURNS INT
BEGIN
RETURN CASE
WHEN MAX(originalYear IS NULL) = 0 THEN MAX(originalYear)
ELSE originalYear
END;
END; //
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment