CoderZone.org

Category: >> mySQL >> MySQL Capitalize Bookmark and Share

<< lastnext >>

Snippet Name: MySQL Capitalize

Description: Capitalizing in MySQL.

Comment: (none)

Author: CoderZone
Language: MYSQL
Highlight Mode: MYSQL
Last Modified: March 01st, 2009

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 ;
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 12