Skip navigation links

MySQL Forums :: Stored Procedures :: Capitalize each word in a string ?


Advanced Search

Re: Capitalize each word in a string ?
Posted by: William Chiquito ()
Date: May 15, 2007 03:18PM

Hi Robert,

Try:
DELIMITER $$

DROP FUNCTION IF EXISTS `ICap`$$

CREATE FUNCTION `ICap`(mystring varchar(1000))
	RETURNS VARCHAR(1000)
BEGIN

DECLARE i INT DEFAULT 1;
DECLARE myc, pc CHAR(1);
DECLARE myoutstring VARCHAR(1000) DEFAULT LOWER(mystring);

WHILE i <= CHAR_LENGTH(mystring) DO
	SET myc = SUBSTRING(mystring, i, 1);
	SET pc = CASE WHEN i = 1 THEN ' ' ELSE SUBSTRING(mystring, i - 1, 1) END;
	IF pc IN (' ', '&', '''', '_', '?', ';', ':', '!', ',', '-', '/', '(', '.') THEN
	    SET myoutstring = INSERT(myoutstring, i, 1, UPPER(myc));
	END IF;
	SET i = i + 1;
END WHILE;

RETURN myoutstring;

END$$

DELIMITER ;

select ICap('dell latitude xyz');

ICap('dell latitude xyz')
-------------------------
Dell Latitude Xyz

Options: ReplyQuote


Subject Views Written By Posted
Capitalize each word in a string ? 22731 Robert Hammer 05/15/2007 11:28AM
Re: Capitalize each word in a string ? 12934 William Chiquito 05/15/2007 03:18PM
Re: Capitalize each word in a string ? 6576 Sebastián Gómez 03/05/2008 07:47AM
Re: Capitalize each word in a string ? 6183 Peter Brawley 03/05/2008 10:22AM
Re: Capitalize each word in a string ? 5550 Hugo Valente 07/25/2008 08:21AM
Re: Capitalize each word in a string ? 5451 Deepika Gupta 08/20/2008 01:32AM
Re: Capitalize each word in a string ? 5321 Jason R 08/24/2008 07:50AM
Re: Jason's answer ^^^^ ++++ ! 4678 Andrew Ayers 09/15/2008 01:33PM
Re: Capitalize each word in a string ? 5344 Joe Zack 11/11/2008 07:11AM
Re: Capitalize each word in a string ? 4239 Jason R 08/24/2008 07:52AM
Re: Capitalize each word in a string ? 3231 Marc Sàbat 05/02/2009 03:57AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.