我如何修改这个InitCap函数,以便当它在字符串的末尾并且在它之前有一个空格时始终将大写AB。
abtest< - 应该取代ab
DELIMITER $$
DROP FUNCTION IF EXISTS `CapitializeFirstCharInEveryWord`$$
CREATE FUNCTION `CapitializeFirstCharInEveryWord`(x char(100)) RETURNS char(100) CHARSET utf8
BEGIN
SET @str='';
SET @l_str='';
WHILE x REGEXP ' ' DO
SELECT SUBSTRING_INDEX(x, ' ', 1) INTO @l_str;
SELECT SUBSTRING(x, LOCATE(' ', x)+1) INTO x;
SELECT CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(@l_str,1,1)),LOWER(SUBSTRING(@l_str,2)))) INTO @str;
END WHILE;
RETURN LTRIM(CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(x,1,1)),LOWER(SUBSTRING(x,2)))));
END$$
答案 0 :(得分:0)
您想在字符串中大写最后一个字吗?你可以用简单的方式做到这一点。评论中的描述。
CREATE FUNCTION `CapitalizeLastWord`(x char(100)) RETURNS char(100) CHARSET utf8
BEGIN
-- detect has x space
SET @space_pos_reverse = LOCATE(' ', REVERSE(x));
-- if not return unchanged x
IF @space_pos_reverse = 0 THEN RETURN x;
END IF;
-- getting last space position
SET @last_space_pos = LENGTH(x)-@space_pos_reverse+1;
-- split x to 2 parts, 2nd part gettin UPPER
RETURN CONCAT(SUBSTRING(x, 1, @last_space_pos), UPPER(SUBSTRING(x, @last_space_pos+1)) );
END$$