DELIMITER $$;
DROP FUNCTION IF EXISTS `radius`.`fun_split`$$
CREATE DEFINER=`root`@`%` FUNCTION `fun_split`(nm varchar(100)) RETURNS varchar(500) CHARSET latin1
READS SQL DATA
BEGIN
declare i int;
declare rtn int;
declare rtn1 int;
declare sec varchar(10);
declare rtnstr varchar(500);
set i=1;
set rtnstr='';
WHILE i <length(nm) DO
set rtn= ascii(substring(nm,i,1));
IF rtn>127 THEN
set rtn1= ascii(right(substring(nm,i,2),1));
IF rtn1> 127 THEN
IF i<length(nm)-1 THEN
IF (ascii(right(substring(nm,i,3),1))>127 and ascii(right(substring(nm,i,4),1))>127) THEN
set sec=concat(right(concat('00',rtn-160),2),right(concat('00',rtn1-160),2),right(concat('00',ascii(right(substring(nm,i,3),1))-160),2),right(concat('00',ascii(right(substring(nm,i,4),1))-160),2),' ');
ELSE
set sec='';
END IF;
ELSE
set sec='';
END IF ;
ELSE
set sec=concat(char(rtn),char(rtn1));
END IF;
SET i = i + 1;
ELSE
IF i<length(nm)-1 THEN
IF (ascii(right(substring(nm,i,2),1))>127 and ascii(right(substring(nm,i,3),1))>127) THEN
set sec=concat(char(rtn),' ');
ELSE
set sec=char(rtn);
END IF;
ELSE
IF (ascii(right(substring(nm,i,2),1))>127 and ascii(right(substring(nm,i,3),1))>127) THEN
set sec=char(rtn);
ELSE
set sec=char(rtn,ascii(right(substring(nm,i,2),1)));
END IF;
END IF;
END IF;
set rtnstr=concat(rtnstr,sec);
SET i = i + 1;
END WHILE;
RETURN rtnstr;
END$$
DELIMITER ;$$