Skip to content

Instantly share code, notes, and snippets.

@gustavo-rodrigues-dev
Created September 5, 2016 21:50
Show Gist options
  • Save gustavo-rodrigues-dev/c12578273de4924588243c250d1ec4ae to your computer and use it in GitHub Desktop.
Save gustavo-rodrigues-dev/c12578273de4924588243c250d1ec4ae to your computer and use it in GitHub Desktop.
remove white spaces into collumn mysql
#http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/
DELIMITER $$
CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,ch);
ELSE
SET temp = CONCAT(temp,replacement);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END$$
DELIMITER ;
SET SQL_SAFE_UPDATES = 0;
UPDATE your_table
SET email = regex_replace('[[:blank:]]','', your_coll)
WHERE your_coll REGEXP '[[:blank:]]' = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment