Skip to content

Instantly share code, notes, and snippets.

@ViCMAP
Last active August 29, 2015 14:14
Show Gist options
  • Save ViCMAP/328be34d8df7ebbc8fb0 to your computer and use it in GitHub Desktop.
Save ViCMAP/328be34d8df7ebbc8fb0 to your computer and use it in GitHub Desktop.
Uppercase the first character of each word in a string using this MySQL custom function.
FUNCTION `ucwords`(input VARCHAR(255)) RETURNS varchar(255) CHARSET utf8 COLLATE utf8_spanish_ci
DETERMINISTIC
BEGIN
DECLARE len INT;
DECLARE i INT;
SET len = CHAR_LENGTH(input);
SET input = LOWER(input);
SET i = 0;
WHILE (i < len) DO
IF (MID(input,i,1) = ' ' OR i = 0) THEN
IF (i < len) THEN
SET input = CONCAT(
LEFT(input,i), UPPER(MID(input,i + 1,1)), RIGHT(input,len - i - 1)
);
END IF;
END IF;
SET i = i + 1;
END WHILE;
RETURN input;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment