Wednesday, March 18, 2009

Function to Capitalize Correctly with 'Mc' or 'Mac'

Select    EName
, Mc(EName)
From Emp
Where EName Like 'M%'

ENAME MC(ENAME)
--------------- ---------------
MCNEIL McNeil
MACINTYRE MacIntyre

MARTIN Martin
MILLER Miller
This is a start at capitalizing names correctly. This function will look at the name and capitalize in two parts if it finds either 'MC' or 'MAC' in the first two to three characters.
Create Or Replace Function Mc(Name Varchar2)
Return Varchar2
Is
Begin
Return(Case ---------------------------------------
-- When the name starts with 'MC' or
-- 'MAC'...
---------------------------------------
When Upper(Substr(Name,1,2))='MC'
Or Upper(Substr(Name,1,3))='MAC'
---------------------------------------
-- InitCap in two sections. The first
-- is up to the first 'C' found. The
-- 2nd is from the first 'C' to the end.
---------------------------------------
Then Initcap
(Substr
(Name,1,
Instr(Upper(Name)
,'C'
,1)
)
)
|| Initcap
(Substr
(Name
,Instr(Upper(Name)
,'C'
,1)+1
)
)
---------------------------------------
-- If not, 'MC' or 'MAC' InitCap the
-- whole name.
---------------------------------------
Else Initcap(Name)
End);
End;
/

No comments:

Post a Comment