CREATE FUNCTION dbo.WordCap(in str VARCHAR(255) ) RETURNS VARCHAR(255)
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INTeger;
DECLARE bool INT;
DECLARE punct CHAR(1);
set i = 1;
set bool = 1;
set punct = ' ';
SET s = LCASE( str );
WHILE i < LENGTH( str )
LOOP
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ENDIF;
IF bool=1 THEN
IF c >= 'a' AND c <= 'z' THEN
SET s = LEFT(s,i-1) || UCASE(c) || SUBSTRING(s,i+1);
SET bool = 0;
ELSE IF c >= '0' AND c <= '9' THEN
SET bool = 0;
ENDIF ENDIF;
ENDIF;
SET i = i+1;
END LOOP;
RETURN s;
END
Contoh Penggunaan
select dbo.WordCap('DUA BELAS RIBU EMPAT RATUS DUA BELAS')
Hasilnya
Dua Belas Ribu Empat Ratus Dua Belas