Skip to content

Instantly share code, notes, and snippets.

@michael01angelo
Last active May 29, 2018 05:13
Show Gist options
  • Select an option

  • Save michael01angelo/d9f8ce4560c54f3d688ef43aae9c95fc to your computer and use it in GitHub Desktop.

Select an option

Save michael01angelo/d9f8ce4560c54f3d688ef43aae9c95fc to your computer and use it in GitHub Desktop.
MySQL Convert Normal Text to SEO text
DELIMITER //
CREATE FUNCTION `ToSEO`(input TEXT)
RETURNS TEXT
BEGIN
DECLARE output TEXT DEFAULT '';
DECLARE iterator INT DEFAULT 1;
WHILE iterator < (LENGTH(input) + 1) DO
IF SUBSTRING(input, iterator, 1) IN (' ', '-')
THEN
SET output = CONCAT(output, '-');
END IF;
IF SUBSTRING(input, iterator, 1) IN
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
THEN
SET output = CONCAT(output, LCASE(SUBSTRING(input, iterator, 1)));
END IF;
SET iterator = iterator + 1;
END WHILE;
RETURN output;
END //
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment