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

DELIMITER $$

DROP FUNCTION IF EXISTS flib.justabc $$
CREATE FUNCTION flib.justabc(dirtystring VARCHAR(500)) RETURNS VARchar(500) charset utf8
BEGIN

  DECLARE goodstring varchar(500);
  DECLARE tempstring varchar(500);
  declare tempchar VARchar(2);
  declare foundcharpos int;
  declare doublespace bit;
  declare i int;

  set goodstring='abcdefghijklmnopqrstuvwxyz';

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

if dirtystring<>'' then

  REPEAT

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

      if LOCATE(tempchar,goodstring)>0 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;

else set tempstring='';

end if;

RETURN tempstring;


END

$$

DELIMITER ;