Skip to content

Instantly share code, notes, and snippets.

@marcelogrsp
Created July 31, 2020 20:09
Show Gist options
  • Save marcelogrsp/1800b73afd09363274279577a3ade9fb to your computer and use it in GitHub Desktop.
Save marcelogrsp/1800b73afd09363274279577a3ade9fb to your computer and use it in GitHub Desktop.
UC_FIRST Capitalize any given string - This function is a clone of the ucfirst function in PHP.
DROP FUNCTION IF EXISTS UC_FIRST;
CREATE FUNCTION UC_FIRST(oldWord VARCHAR(255)) RETURNS VARCHAR(255)
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2));
UC_DELIMITER Capitalize with a delimiter in between words
DROP FUNCTION IF EXISTS UC_DELIMITER;
DELIMITER //
CREATE FUNCTION UC_DELIMITER(
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 ;
Examples:
SELECT UC_DELIMITER('eric-leroy','-',TRUE);
Eric-Leroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment