2

How can I replace multiple spaces to a single space in a mysql string.

2 Answers 2

2
DELIMITER $$

CREATE PROCEDURE I_hate_duplicate_spaces
BEGIN
  DECLARE rows_affected INTEGER;

  REPEAT
    UPDATE table1 SET afield = REPLACE(afield,'  ',' ');
    SELECT ROW_COUNT() INTO rows_affected;
  UNTIL rows_affected = 0 END REPEAT;
END $$

DELIMITER ;

Now start the stored procedure:

CALL I_hate_duplicate_spaces;

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

Sign up to request clarification or add additional context in comments.

Comments

1

Essentially you want to do a regular expression replacement in MySQL something like PHP's preg_replace('/ +/', ' ', $sString) - unfortunately this isn't built into MySQL so you'd need a user-defined function ... which led me to this: How to do a regular expression replace in MySQL?

Hope that helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.