Had a problem with a database (surprise!), that contained names. Names were entered by a variety of persons/programs, so we got a lump of stuff like:
DAVID
david
j. wilson
Franklin
...along with last names like:
VAN HORN
Johnson
wilson
None of which looked too good when it came time to print things out for them. While there is an easy method of plain upper-casing things, for things like "j. wilson" it didn't work right. It would upper-case the J, but not the W in "Wilson", same for last names.
Bang this in to your DB console:
Code:
DELIMITER ||
CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255)
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INT DEFAULT 1;
DECLARE bool INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ELSEIF bool=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET bool = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET bool = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END ||
DELIMITER ;
...and run it with something like "update table set firstname = UC_Words(firstname);"
And you'll wind up with "J. Wilson" and "Van Horn". Much nicer.