create table stuckTogether
( id int auto_increment primary key,
String varchar(200) not null
);
insert stuckTogether(String) values
('FannieMae'),
('FreddyMac'),
('ArthurAndersen'),
('DJMurphyZ');
Function:
DROP FUNCTION IF EXISTS separateStuck;
DELIMITER $$
CREATE FUNCTION separateStuck
( s VARCHAR(200)
)
RETURNS VARCHAR(200)
BEGIN
DECLARE sOut VARCHAR(200) DEFAULT '';
DECLARE iSize,iPos INT;
SET iSize=LENGTH(s);
SET iPos=1;
label1: WHILE iPos<=iSize DO
SET sOut=CONCAT(sOut,SUBSTRING(s,iPos,1));
IF ASCII(SUBSTRING(s,iPos,1)) BETWEEN 97 and 122 THEN
-- it is lowercase
IF iPos<iSize THEN
IF ASCII(SUBSTRING(s,iPos+1,1)) BETWEEN 65 and 90 THEN
-- the next one is upper case
SET sOut=CONCAT(sOut,' ');
END IF;
END IF;
END IF;
SET iPos=iPos+1;
END WHILE label1;
RETURN sOut;
END;$$
DELIMITER ;
Test:
select id,separateStuck(String) as String
from stuckTogether;
+----+-----------------+
| id | String |
+----+-----------------+
| 1 | Fannie Mae |
| 2 | Freddy Mac |
| 3 | Arthur Andersen |
| 4 | DJMurphy Z |
+----+-----------------+