Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como posso obter a funcionalidade initcap no MySQL?


Algum tempo atrás eu estava procurando por uma função initcap/ucfirst embutida no MySQL, mas infelizmente não consegui encontrar essas funções de string, então decidi escrever a minha própria .. graças ao membro da comunidade MySQL que corrigiu o bug na minha função e postou de volta .
DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`initcap`$$

CREATE FUNCTION `initcap`(x char(30)) RETURNS char(30) CHARSET utf8
READS SQL DATA
DETERMINISTIC
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: 

select initcap('This is test string');