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!

25 thoughts on “MySQL Capitalize Function

  1. Pingback: Capitalize the first letter of each word using MYSQL - Microsoft Windows Vista Community Forums - Vistaheads

  2. Muhammad Abubakar

    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 ;

    Reply
  3. Pingback: Capitalize the first letter of each word using MYSQL | keyongtech

  4. Jeremy Woertink

    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 :)

    Reply
  5. Jools

    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?

    Reply
  6. Jaime

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

    Reply
  7. Jeff

    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.

    Reply
  8. Pingback: Fungsi MySQL Huruf Kapital Pada Awal Kata » Mangifero Scratchlog

  9. Pingback: Anonymous

  10. Hunter Monk

    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

    Reply
  11. Stefano P

    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 ;

    Reply
  12. Alex D

    thanks for the script ;)

    is it possible to modify the script to accept an additional parameter for capitalizing words that are longer than X characters? The script by Stefano doesn’t work

    Thanks
    Alex

    Reply
  13. me Post author

    No problem, @Alex D

    All you have to do is modify the arguments to take in a length:

    CREATE FUNCTION CAP_FIRST (input VARCHAR(255), maxLength int)

    and then add an if:
    IF LENGTH(InputString) > maxLength THEN
    SET InputString = CONCAT( LEFT(InputString,Last_Word),
    END IF

    Reply
  14. SF

    @me Can please post the full working script that incorporates maxLength? Your post seems to imply tweaking the initial script but then the If block seems to imply using the other, non-working one. Thanks!

    Reply
  15. SF

    – Capitalizes the first letter of every word where word has more than 2 chars and is not “the” or “and”.

    DELIMITER //

    DROP FUNCTION IF EXISTS CAP_FIRST;
    CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
    RETURNS VARCHAR(255)

    BEGIN
    DECLARE len INT;
    DECLARE i INT;
    DECLARE j INT;
    DECLARE wordLength INT;
    DECLARE word VARCHAR(255);
    DECLARE letter CHAR(1);
    DECLARE str VARCHAR(255);

    SET len = CHAR_LENGTH(input);
    SET i = 0;
    SET str = ”;

    WHILE (i < len) DO
    IF (MID(input,i,1) = ' ' OR i = 0) THEN
    IF (i < len) THEN
    — look ahead for word length
    SET j = 1;
    SET word = '';
    wordLoop: WHILE (i+j-1 2 AND word != ‘and’ AND word != ‘the’) THEN
    SET input = CONCAT(
    LEFT(input,i),
    UPPER(MID(input,i + 1,1)),
    RIGHT(input,len – i – 1)
    );
    END IF;
    END IF;
    END IF;
    SET i = i + 1;
    END WHILE;

    – RETURN str;
    RETURN input;
    END //

    DELIMITER ;

    Reply
  16. Reuben

    This is my version with exempt words. This version has mysql return each word in the string to check against. It can be easily adapted to word length restriction if needed.

    DELIMITER $$

    DROP FUNCTION IF EXISTS `CAP_FIRST`$$

    CREATE FUNCTION `CAP_FIRST`(input VARCHAR(255))
    RETURNS VARCHAR(255) DETERMINISTIC

    BEGIN
    DECLARE endstring INT;
    DECLARE i INT;
    DECLARE word VARCHAR(255);
    DECLARE output VARCHAR(255);

    SET input = CONCAT(LOWER(input),’ ||’);
    SET i = 1;
    SET endstring = 0;
    SET word = ”;
    SET output = ”;

    WHILE (endstring = 0) DO
    SET word = SUBSTRING_INDEX(SUBSTRING_INDEX(input, ‘ ‘,i),’ ‘,-1);
    IF (word != ‘||’) THEN
    IF (word NOT IN (‘and’,'with’,'for’,'a’)) THEN
    SET word = CONCAT(UPPER(LEFT(word,1)),MID(word,2));
    END IF;
    SET output = CONCAT(output,’ ‘,word);
    ELSE
    SET endstring = 1;
    END IF;
    SET i = i + 1;
    END WHILE;

    RETURN output;
    END$$

    DELIMITER ;

    Reply
  17. Pingback: How to capitalize first letters of words in MySQL | Local Wisdom, Inc.

  18. Atul Sohan

    Hey Changed the Delimiter and it worked like a Breeze
    posting your original Code with THe DELIMITER
    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;
    //

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>