Created
August 24, 2016 18:17
-
-
Save simonds/78f8eab4c565f6e8340caa5c9083a3e1 to your computer and use it in GitHub Desktop.
Found this MySQL function in several places online, so I don't know who to credit. This works a treat outputting Uppercase first letters on multiple words within a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
UPDATE Table_name SET column_name = UC_Words(column_name) | |
*/ | |
DELIMITER || | |
CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET utf8_general_ci | |
BEGIN | |
DECLARE c CHAR(1); | |
DECLARE s VARCHAR(255); | |
DECLARE i INT DEFAULT 1; | |
DECLARE bool INT DEFAULT 1; | |
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/'; | |
SET s = LCASE( str ); | |
WHILE i < LENGTH( str ) DO | |
BEGIN | |
SET c = SUBSTRING( s, i, 1 ); | |
IF LOCATE( c, punct ) > 0 THEN | |
SET bool = 1; | |
ELSEIF bool=1 THEN | |
BEGIN | |
IF c >= 'a' AND c <= 'z' THEN | |
BEGIN | |
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1)); | |
SET bool = 0; | |
END; | |
ELSEIF c >= '0' AND c <= '9' THEN | |
SET bool = 0; | |
END IF; | |
END; | |
END IF; | |
SET i = i+1; | |
END; | |
END WHILE; | |
RETURN s; | |
END || | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment