MySQL - Stored Function - Uppercase First Character

This MySQL stored function can be used to change the first character in a string to uppercase much like PHP's ucfirst() function.
  1. DROP FUNCTION IF EXISTS uc_first;  
  2.   
  3. DELIMITER $$  
  4.   
  5. CREATE FUNCTION uc_first(str LONGTEXT)     
  6.     RETURNS LONGTEXT     
  7.     BEGIN     
  8.         SET @firstChar = SUBSTRING(str, 1,1);     
  9.         SET @remainingChars = SUBSTRING(str, 2);     
  10.         RETURN CONCAT(UPPER(@firstChar), @remainingChars);     
  11.     END  
  12.   
  13. $$  
  14.   
  15. DELIMITER ;  

No comments:

Post a Comment