Skip to content

Instantly share code, notes, and snippets.

@kidsil
Created August 24, 2010 13:15
Show Gist options
  • Save kidsil/547535 to your computer and use it in GitHub Desktop.
Save kidsil/547535 to your computer and use it in GitHub Desktop.
# Strip non Alphanumeric (_ and -) from whatever field you want on MySQL
CREATE FUNCTION `strip_non_alpha`(_dirty_string CHAR(127)) RETURNS char(127) CHARSET utf8
BEGIN
DECLARE _length int;
DECLARE _position int;
DECLARE _current_char varchar(1);
DECLARE _clean_string varchar(40);
SET _clean_string = '';
SET _length = LENGTH(_dirty_string);
SET _position = 1;
WHILE _position <= _length DO
SET _current_char = SUBSTRING(_dirty_string, _position, 1);
IF _current_char REGEXP '[A-Za-z0-9_\-]' THEN
SET _clean_string = CONCAT(_clean_string, _current_char);
END IF;
SET _position = _position + 1;
END WHILE;
RETURN CONCAT('', _clean_string);
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment