Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A839E1337B48 for ; Thu, 23 Sep 2010 11:18:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 45297-03 for ; Thu, 23 Sep 2010 14:18:33 +0000 (UTC) X-Greylist: delayed 00:05:25.029841 by SQLgrey-1.7.6 Received: from nm27-vm0.bullet.mail.ac4.yahoo.com (nm27-vm0.bullet.mail.ac4.yahoo.com [98.139.52.244]) by mail.postgresql.org (Postfix) with SMTP id 41A6C133798E for ; Thu, 23 Sep 2010 11:18:32 -0300 (ADT) Received: from [98.139.52.192] by nm27.bullet.mail.ac4.yahoo.com with NNFMP; 23 Sep 2010 14:13:07 -0000 Received: from [74.6.228.50] by tm5.bullet.mail.ac4.yahoo.com with NNFMP; 23 Sep 2010 14:13:07 -0000 Received: from [127.0.0.1] by smtp109.mail.ac4.yahoo.com with NNFMP; 23 Sep 2010 14:13:07 -0000 X-Yahoo-Newman-Id: 85342.77282.bm@smtp109.mail.ac4.yahoo.com Received: from jonathanlaptop (JB@67.34.121.27 with login) by smtp109.mail.ac4.yahoo.com with SMTP; 23 Sep 2010 07:13:06 -0700 PDT X-Yahoo-SMTP: 44.80yyswBBWn2mP4eIs.OiF0CguW7wtDDCwTEpCAZ4GQXK1x4I- X-YMail-OSG: MTEG49MVM1lWf7AeldV44Cd5Yk7nlEpzEy8JbGg4sMhk07W 0kgxhWmpdjUfxNlC6Pb4wfWtNgstVAoLiEiMuShifPwMLR3vTHD4gVUByAIs aQXLuFdxwoL0embMT6jxLvH78U9RiB.FE9XfqzH1i8cY9AiaSOspxTXoL_i6 34a9W2.eZebfzkEL8Cse0uTJv6Kd4dFA9iie1b.HpeMVBLaOEfhKTX2kVMWm e3NOtt2tabQKfK9mhWwvrY7hZBVsy X-Yahoo-Newman-Property: ymail-3 Reply-To: From: "Jonathan Brinkman" To: Cc: Subject: Re: Proper case function Date: Thu, 23 Sep 2010 10:13:05 -0400 Organization: BlackSky Technologies, Inc. Message-ID: <000b01cb5b29$715d0760$54171620$@com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: ActbKSehapEq6XuGSKuyT6zFWOcg6A== Content-Language: en-us X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, UNPARSEABLE_RELAY=0.001 X-Spam-Level: X-Archive-Number: 201009/89 X-Sequence-Number: 34620 Here is a simple title-case function for Postgresql. Best, Jonathan CREATE OR REPLACE FUNCTION "format_titlecase" ( "v_inputstring" varchar ) RETURNS varchar AS $body$ /* select * from Format_TitleCase('MR DOG BREATH'); select * from Format_TitleCase('each word, mcclure of this string:shall be transformed'); select * from Format_TitleCase(' EACH WORD HERE SHALL BE TRANSFORMED TOO incl. mcdonald o''neil o''malley mcdervet'); select * from Format_TitleCase('mcclure and others'); select * from Format_TitleCase('J & B ART'); select * from Format_TitleCase('J&B ART'); select * from Format_TitleCase('J&B ART J & B ART this''s art''s house''s problem''s 0''shay o''should work''s EACH WORD HERE SHALL BE TRANSFORMED TOO incl. mcdonald o''neil o''malley mcdervet'); */ DECLARE v_Index INTEGER; v_Char CHAR(1); v_OutputString VARCHAR(4000); SWV_InputString VARCHAR(4000); BEGIN SWV_InputString := v_InputString; SWV_InputString := LTRIM(RTRIM(SWV_InputString)); --cures problem where string starts with blank space v_OutputString := LOWER(SWV_InputString); v_Index := 1; v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,1,1)) from 1 for 1); -- replaces 1st char of Output with uppercase of 1st char from Input WHILE v_Index <= LENGTH(SWV_InputString) LOOP v_Char := SUBSTR(SWV_InputString,v_Index,1); -- gets loop's working character IF v_Char IN('m','M',' ',';',':','!','?',',','.','_','-','/','&','''','(',CHR(9)) then --END4 IF v_Index+1 <= LENGTH(SWV_InputString) then IF v_Char = '''' AND UPPER(SUBSTR(SWV_InputString,v_Index+1,1)) <> 'S' AND SUBSTR(SWV_InputString,v_Index+2,1) <> REPEAT(' ',1) then -- if the working char is an apost and the letter after that is not S v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index+1,1)) from v_Index+1 for 1); ELSE IF v_Char = '&' then -- if the working char is an & IF(SUBSTR(SWV_InputString,v_Index+1,1)) = ' ' then v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index+2,1)) from v_Index+2 for 1); ELSE v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index+1,1)) from v_Index+1 for 1); END IF; ELSE IF UPPER(v_Char) != 'M' AND (SUBSTR(SWV_InputString,v_Index+1,1) <> REPEAT(' ',1) AND SUBSTR(SWV_InputString,v_Index+2,1) <> REPEAT(' ',1)) then v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index+1,1)) from v_Index+1 for 1); END IF; END IF; END IF; -- special case for handling "Mc" as in McDonald IF UPPER(v_Char) = 'M' AND UPPER(SUBSTR(SWV_InputString,v_Index+1,1)) = 'C' then v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index,1)) from v_Index for 1); --MAKES THE C LOWER CASE. v_OutputString := OVERLAY(v_OutputString placing LOWER(SUBSTR(SWV_InputString,v_Index+1,1)) from v_Index+1 for 1); -- makes the letter after the C UPPER case v_OutputString := OVERLAY(v_OutputString placing UPPER(SUBSTR(SWV_InputString,v_Index+2,1)) from v_Index+2 for 1); --WE TOOK CARE OF THE CHAR AFTER THE C (we handled 2 letters instead of only 1 as usual), SO WE NEED TO ADVANCE. v_Index := v_Index+1; END IF; END IF; END IF; --END3 v_Index := v_Index+1; END LOOP; --END2 RETURN coalesce(v_OutputString,''); END; $body$ LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER COST 100;