Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Small function for Title Case

garbuyaJan 7 2013 — edited Jan 8 2013
Even if Oracle has INITCAP it is not formatiing text properly and makes first letter after apostrophies in upper case.
The following small function takes care of it and also has option to replace non-printable chars with spaces and capitalize "irish like" names to make letter after "Mc" or "Mac' in upper case

{code}
create or replace
FUNCTION FN_TITLE_CASE
( P_TXT VARCHAR2
,P_REPLACE_NON_PRINTABLE CHAR DEFAULT 'Y'
,P_UPPER_AFTER_MC_AND_MAC CHAR DEFAULT 'Y'
) RETURN VARCHAR2
IS
/*********************************************************************************************************************
Purpose: Format input text to remove non-printable chars and return result in Title Case considering apostrophizes

The difference from INITCAP:
Input: mcdonald,o'malley and ol'der macdonald likes ken's food at o'hare's oasis; it's near mc'donald's
INITCAP: Mcdonald,O'Malley And Ol'Der Macdonald Likes Ken'S Food At O'Hare'S Oasis; It'S Near Mc'Donald'S
FUNCTION: McDonald,O'Malley And Ol'der MacDonald Likes Ken's Food At O'Hare's Oasis; It's Near Mc'Donald's
____________-______________________-______-________________-________________-___________-________________-

*********************************************************************************************************************/
V_TXT VARCHAR2(4000) := TRIM ( UPPER(p_txt) );

BEGIN
IF UPPER(P_REPLACE_NON_PRINTABLE) = 'Y' THEN
V_TXT := REGEXP_REPLACE( V_TXT, '[^'||CHR(32)||'-'||CHR(126)||']', ' ');
END IF;

IF UPPER(P_UPPER_AFTER_MC_AND_MAC) = 'Y' THEN
V_TXT := REGEXP_REPLACE(V_TXT, '(^| )(MA?C)', '\1\2'||CHR(247));
END IF;

V_TXT := REGEXP_REPLACE( V_TXT, '([[:alnum:]]{2,})('')([[:alnum:]*])', '\1\2'||CHR(254)||'\3' );

V_TXT := REGEXP_REPLACE ( INITCAP(V_TXT), CHR(222)||'|'||CHR(247)||'|'||CHR(254), '');

RETURN (V_TXT);

END;
{code}

If you can provide more elegant solution please do
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 5 2013
Added on Jan 7 2013
3 comments
919 views