MySQL - Capital first letter of each word
<p>Below is MySQL function to capitalize the first letter of every word in a string.</p>

<pre><code>  USE `db`$$

DROP FUNCTION IF EXISTS `UC_Words`$$

CREATE DEFINER=`root`@`localhost` FUNCTION `UC_Words`(str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET latin1
    MODIFIES SQL DATA
    DETERMINISTIC
BEGIN   
DECLARE c CHAR(1);  
  DECLARE s VARCHAR(255);  
  DECLARE i INT DEFAULT 1;  
  DECLARE BOOL INT DEFAULT 1;  
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';  
  SET s = LCASE( str );  
  WHILE i &lt; LENGTH( str ) DO  
     BEGIN  
       SET c = SUBSTRING( s, i, 1 );  
       IF LOCATE( c, punct ) &gt; 0 THEN  
        SET BOOL = 1;  
      ELSEIF BOOL=1 THEN  
        BEGIN  
          IF c &gt;= 'a' AND c &lt;= 'z' THEN  
             BEGIN  
               SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));  
               SET BOOL = 0;  
             END;  
           ELSEIF c &gt;= '0' AND c &lt;= '9' THEN  
            SET BOOL = 0;  
          END IF;  
        END;  
      END IF;  
      SET i = i+1;  
    END;  
  END WHILE;  
  RETURN s;  

    END$$

DELIMITER ;
</code></pre>

<p>It's works fine for all Strings. But if I provide string  "M R E" then its give "M R e" output.</p>

<p>plz suggest.</p>