Technical Article

Set Proper Name Case Function

,

This script is basic in terms of how much it can handle. It sets the proper case in a "full name" field (i.e. First, Middle and Last names all in one column such as 'BILL MCDOUGLE' or 'terry smith-wenderling'. The function will take any case and set it appropriately including last names with the 'Mc' prefix.

There are some limitations.

1) It can't handle middle names if there is a last name prefix of 'Mc' such as 'McDougle'.

2) I haven't spent any additional time with other last name prefixes such as 'O' in "O'Brien" or 'Di' in 'DiNatale'

3) It is unable to handle bad data such as hyphens at the end of the string with only a single name.

-- =============================================
-- Create fn_NameCase
-- Author:   Steven Kielkucki
-- Date:     February 19, 2006
-- Description: Function for capitalizing names
-- Updates:
-- =============================================
IF EXISTS (SELECT * 
   FROM   sysobjects 
   WHERE  name = N'fn_NameCase')
DROP FUNCTION fn_NameCase
GO

CREATE FUNCTION fn_NameCase 
(@Name varchar(99)
  )
RETURNS varchar(99)
AS
BEGIN

DECLARE @TrimmedName varchar(99)

SELECT @TrimmedName = LTRIM(RTRIM(@Name))


IF CHARINDEX(' ', @TrimmedName, 1) > 1
BEGIN

  SELECT @Name = UPPER(LEFT(@TrimmedName, 1)) 
  + LOWER(SUBSTRING(@TrimmedName, 2, CHARINDEX(' ',@TrimmedName , 1)-1))
  + UPPER(SUBSTRING(@TrimmedName, CHARINDEX(' ',@TrimmedName , 1) + 1, 1))
  + CASE WHEN PATINDEX('%mc%', @TrimmedName) > CHARINDEX(' ',@TrimmedName , 1) THEN 
              LOWER(SUBSTRING(@TrimmedName, PATINDEX('%mc%', @TrimmedName) + 1 , 1)) +
              UPPER(SUBSTRING(@TrimmedName, PATINDEX('%mc%', @TrimmedName) + 2, 1)) +
              LOWER(SUBSTRING(@TrimmedName, PATINDEX('%mc%', @TrimmedName) + 3, LEN(@TrimmedName)
                    - PATINDEX('%mc%', @TrimmedName)))
         WHEN CHARINDEX('-',@TrimmedName, 1) > CHARINDEX(' ',@TrimmedName , 1) THEN
              LOWER(SUBSTRING(@TrimmedName, 
                              CHARINDEX(' ',@TrimmedName , 1) + 2,
                              CHARINDEX('-',@TrimmedName , 1) 
                                - (CHARINDEX(' ',@TrimmedName , 1) + 2))) +
              UPPER(SUBSTRING(@TrimmedName, CHARINDEX('-', @TrimmedName, 1), 2)) +
              LOWER(SUBSTRING(@TrimmedName, CHARINDEX('-',@TrimmedName , 1) + 2,
                   (LEN(@TrimmedName)-1) - CHARINDEX('-',@TrimmedName , 1)))
         WHEN LEN(@TrimmedName) - CHARINDEX(' ', REVERSE(@TrimmedName), 1) > CHARINDEX(' ',@TrimmedName , 1) THEN
              LOWER(SUBSTRING(@TrimmedName, 
                              CHARINDEX(' ',@TrimmedName , 1) + 2,
                              LEN(@TrimmedName) - (CHARINDEX(' ', REVERSE(@TrimmedName), 1)-1) - (CHARINDEX(' ',@TrimmedName , 1) + 2))) + 
              UPPER(SUBSTRING(@TrimmedName,
                              LEN(@TrimmedName) - (CHARINDEX(' ', REVERSE(@TrimmedName), 1)-1), 2)) +
              LOWER(SUBSTRING(@TrimmedName,
                              LEN(@TrimmedName) - (CHARINDEX(' ', REVERSE(@TrimmedName), 1)-3),
                              LEN(@TrimmedName) - (CHARINDEX(' ', REVERSE(@TrimmedName), 1)-3)))
         ELSE LOWER(SUBSTRING(@TrimmedName, CHARINDEX(' ',@TrimmedName , 1) + 2,
                   (LEN(@TrimmedName)-1) - CHARINDEX(' ',@TrimmedName , 1)))
    END

END
RETURN @Name


END
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating