JOEZACK.COM Code Musings and Such

20Oct/0813

MySQL Capitalize Function

I wrote a little MySQL function to capitalize the first letter of every word in a string. I thought I'd share since I wasn't able to google for it.

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;

So running the following code...

SELECT	CAP_FIRST(
	'this is totally like   @ TEST 1 right!'
)

Returns the string "This Is Totally Like @ Test 1 Right!"

I would rather have regex'd it, but I couldn't find any sort of regex replace function in the docs.

Download the code!

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: Leave a comment
Comments (13) Trackbacks (4)
  1. AOA
    Thanks Friend for this.

    Here is my modified version.

    DELIMITER $$

    DROP FUNCTION IF EXISTS `sharex`.`Capitalize`$$

    CREATE DEFINER=`root`@`%` FUNCTION `Capitalize`(InputString VARCHAR(255)) RETURNS VARCHAR(255) CHARSET utf8
    BEGIN
    DECLARE Input_Length INT;
    DECLARE Count INT;

    SET Input_Length = CHAR_LENGTH(InputString);
    SET InputString = LOWER(InputString);
    SET Count = 0;

    WHILE (Count < Input_Length) DO
    IF (MID(InputString,Count,1) = ‘ ‘ OR MID(InputString,Count,1) = ‘.’ OR Count = 0) THEN
    SET InputString = CONCAT(LEFT(InputString,Count),UPPER(MID(InputString,Count + 1,1)),
    RIGHT(InputString,Input_Length – Count – 1));
    ELSE
    SET InputString = CONCAT(LEFT(InputString,Count),LOWER(MID(InputString,Count + 1,1)),
    RIGHT(InputString,Input_Length – Count – 1));
    END IF;
    SET Count = Count + 1;
    END WHILE;

    RETURN InputString;

    END$$

    DELIMITER ;

  2. Awesome function JOEZACK! Works like a charm. Thanks for sharing!

  3. JOEZACK thanks mate, needed this exact thing and works perfect.

    thanks again

  4. DUDE! This layout is awesome. I’m diggin’ the chrome style. Oh, and on a side note, you’re a sick sick individual for writing SQL like that :p lol. Thanks, this is going to help out :)

  5. Hi JOEZACK,

    It’s probably me but when I get down to;
    DECLARE len INT;

    It bombs with a mysql error…

    Have I missed something?

  6. You are the man, I was testing another functions supposed to do this, and they was failing with non alfabetic characters

  7. Thanks a lot for this. Really useful…

    I hope you don’t mind if I publish this on my blog too? I will link back to you, of course…

  8. No problem, glad I could help!

  9. I’m with Jools.. bombs on DECLARE len INT;

    – Nice effort though.

  10. Great script. Worked like a charm!

    For you guys that are getting the mysql error…

    The default delimiter in mysql is shown at the bottom of the sql query window as a semi-colon. You have to change that. Make it a tilde and the script will work perfectly.

  11. Hallo, Neat Post. Es gibt ein Problem mit Ihrer Website in Internet Explorer, würde dies zu testen … also noch ist der Marktführer und ein großer Teil der Menschen wird Ihre wunderbare schriftlich wegen dieses Problems vermissen.

  12. This works perfectly, thanks!

    It it possible to apply this to an entire column inside a table? In a table, I have a column with a string inside it, and I need to capitalize the first letter of every word.

    Is this possible using your function?

    Thanks,
    Hunter

  13. Slightly modified version of your code, to capitalize first char only for words longer than X character.
    Una versione leggermente modificata per considerare solo le parole più lunghe di X caratteri.

    DELIMITER $$

    DROP FUNCTION IF EXISTS Capitalize$$

    CREATE FUNCTION Capitalize (InputString VARCHAR(255)) RETURNS VARCHAR(255) CHARSET utf8
    BEGIN
    DECLARE Input_Length INT;
    DECLARE Count INT;
    DECLARE Last_Word INT;
    DECLARE Min_Char INT;

    SET Input_Length = CHAR_LENGTH(InputString);
    SET InputString = LOWER(InputString);
    SET Count = 0;
    SET Last_Word = 0;
    SET Min_Char = 3;

    WHILE (Count Min_Char) THEN
    SET InputString = CONCAT( LEFT(InputString,Last_Word),
    UPPER(MID(InputString,Last_Word + 1,1)),
    RIGHT(InputString,Input_Length – Last_Word – 1));
    END IF;
    SET Last_Word = Count;
    END IF;
    SET Count = Count + 1;
    END WHILE;

    RETURN InputString;
    END$$

    DELIMITER ;


Leave a comment

(required)