Pages

UCFirst Function for MySQL

Below is a MySQL implementation of PHP’s ucfirst function which capitalizes the first letter of each word in a string
DELIMITER $$
CREATE FUNCTION CAP_FIRST (INPUT VARCHAR(255))
RETURNS VARCHAR(255)
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$$
DELIMITER ;

Use

SELECT CAP_FIRST('my string of words');
Source : http://www.flynsarmy.com/2011/12/ucfirst-function-for-mysql/