-- Generation Time: Oct 16, 2007
-- Last update: Oct 16, 2007
-- Database: `flib`
--
-- BY JULIO RAFFO (EPFL/CEMI)
--
-- --------------------------------------------------------


DELIMITER $$

DROP FUNCTION IF EXISTS flib.dedouble $$
CREATE FUNCTION  flib.dedouble(dirtystring VARCHAR(500)) RETURNS varchar(500) CHARSET utf8
BEGIN

  DECLARE tempstring varchar(500);
  declare tempchar VARchar(2);
  declare i int;


  DECLARE goodstring varchar(500);
  declare foundcharpos int;
  declare doublespace bit;

  set i=1;
  set tempstring='';
  set doublespace=0;

if dirtystring<>'' then

  REPEAT

    SET tempchar=mid(dirtystring, i,1);

    if tempchar<>' ' then set tempstring=concat(tempstring, tempchar); set doublespace=0;

      elseif doublespace=0 then set tempstring=concat(tempstring, ' '); set doublespace=1;

    end if;

    set i=i+1;

  UNTIL i>char_length(dirtystring) END REPEAT;

end if;

RETURN trim(tempstring);

END;

 $$

DELIMITER ;