Skip to content

Instantly share code, notes, and snippets.

@NavidMitchell
Last active May 16, 2018 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NavidMitchell/989a14563e1a56f1b88bcb9f05e37710 to your computer and use it in GitHub Desktop.
Save NavidMitchell/989a14563e1a56f1b88bcb9f05e37710 to your computer and use it in GitHub Desktop.
MySQL First letter of each word uppercase
// Mysql function to make begining of word uppercase
// https://www.thingy-ma-jig.co.uk/blog/30-09-2010/mysql-how-upper-case-words
DROP FUNCTION IF EXISTS UC_DELIMETER;
DELIMITER //
CREATE FUNCTION UC_DELIMETER(oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL) RETURNS VARCHAR(255)
BEGIN
SET @oldString := oldName;
SET @newString := "";
tokenLoop: LOOP
IF trimSpaces THEN SET @oldString := TRIM(BOTH " " FROM @oldString); END IF;
SET @splitPoint := LOCATE(delim, @oldString);
IF @splitPoint = 0 THEN
SET @newString := CONCAT(@newString, UC_FIRST(@oldString));
LEAVE tokenLoop;
END IF;
SET @newString := CONCAT(@newString, UC_FIRST(SUBSTRING(@oldString, 1, @splitPoint)));
SET @oldString := SUBSTRING(@oldString, @splitPoint+1);
END LOOP tokenLoop;
RETURN @newString;
END//
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment