DELIMITER $$ DROP FUNCTION IF EXISTS `initcap` $$ CREATE FUNCTION `initcap`(x char(30)) RETURNS char(30) CHARSET utf8 BEGIN SET @str=''; SET @l_str=''; WHILE x REGEXP ' ' DO SELECT SUBSTRING_INDEX(x, ' ', 1) INTO @l_str; SELECT SUBSTRING(x, LOCATE(' ', x)+1) INTO x; SELECT CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(@l_str,1,1)),LOWER(SUBSTRING(@l_str,2)))) INTO @str; END WHILE; RETURN LTRIM(CONCAT(@str, ' ', CONCAT(UPPER(SUBSTRING(x,1,1)),LOWER(SUBSTRING(x,2))))); END $$ DELIMITER ;
Usage of this function would be as follows:-
- SELECT initcap('new delhi') returns "New Delhi"
Saving the article for my reference and for a lot of other people who might be looking for something similar.