有没有办法在MySQL中重新处理条目,如下所示? (在大写字符之前放置一个空格)我想不出对此的答案,似乎SQL并不是真的为这种东西而构建。任何想法都赞赏!!
示例输入:
| id |字符串|
| 1 | FannieMae |
| 2 | FreddyMac |
| 3 | ArthurAndersen |
示例输出:
| id |字符串|
| 1 | Fannie Mae |
| 2 | Freddy Mac |
| 3 |亚瑟安徒生|
答案 0 :(得分:1)
此功能可以满足您的需求:
DELIMITER $$
CREATE FUNCTION `InsertSpace`(s1 VARCHAR(1000))
RETURNS varchar(1000)
BEGIN
DECLARE rs VARCHAR(1000);
DECLARE ch BINARY;
DECLARE i int;
SET rs = SUBSTRING(s1,1,1);
SET i = 2;
label1: LOOP
SET ch = SUBSTRING(s1,i,1);
IF ((ch >= 'A') AND (ch <= 'Z')) THEN
SET rs = CONCAT(rs, ' ');
END IF;
SET rs = CONCAT(rs, ch);
SET i = i + 1;
IF i > LENGTH(s1) THEN
LEAVE label1;
END IF;
END LOOP label1;
RETURN rs;
END
注意ch声明为BINARY - 强制进行区分大小写的比较。
然后使用
UPDATE myTable SET `String`=InsertSpace(`String`);
答案 1 :(得分:1)
create table stuckTogether
( id int auto_increment primary key,
String varchar(200) not null
);
insert stuckTogether(String) values
('FannieMae'),
('FreddyMac'),
('ArthurAndersen'),
('DJMurphyZ');
功能:
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 ;
测试:
select id,separateStuck(String) as String
from stuckTogether;
+----+-----------------+
| id | String |
+----+-----------------+
| 1 | Fannie Mae |
| 2 | Freddy Mac |
| 3 | Arthur Andersen |
| 4 | DJMurphy Z |
+----+-----------------+