CREATE FUNCTION clean_spaces(str VARCHAR(255)) RETURNS VARCHAR(255) BEGIN while instr(str, ' ') > 0 do set str := replace(str, ' ', ' '); end while; return trim(str); END