MySQL Forums
Forum List  »  Stored Procedures

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
Re: Capitalize each word in a string ?
14428
May 15, 2007 03:18PM
5757
August 24, 2008 07:50AM
5066
September 15, 2008 01:33PM
6375
November 11, 2008 07:11AM
4634
August 24, 2008 07:52AM


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.