This MySQL stored function converts the first character in all words found in a sentence to uppercase.
DELIMITER $$
CREATE FUNCTION uc_words(str LONGTEXT)
RETURNS LONGTEXT
BEGIN
DECLARE returnStr LONGTEXT DEFAULT '';
DECLARE len INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
DECLARE ch CHAR(1);
DECLARE m bool DEFAULT true;
SET len = LENGTH(str) + 1;
WHILE (i < len) DO
BEGIN
SET ch = SUBSTRING(str, i, 1);
IF m = true THEN
BEGIN
SET returnStr = CONCAT(returnStr, UCASE(ch));
SET m = FALSE;
END;
ELSEIF ch = ' ' THEN
BEGIN
SET returnStr = CONCAT(returnStr, ' ');
SET m = TRUE;
END;
ELSE
BEGIN
SET returnStr = CONCAT(returnStr, ch);
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN returnStr;
END
$$
DELIMITER ;
No comments:
Post a Comment