Jayme Davis' WebLog

Playing games so you don't have too...

ParseNameString

Well, I'm pretty sure someone is going to tell me I didn't have to do this complicated way, but.. too late! :) I looked around the internet for a SQL parsing function that would parse to proper capitilization, but no luck. I'm lazy, so I decided too write it...

All the name fields in a SQL database I'm working with are all ucase. I wrote this SQL function to parse out proper capitilization for spaces, hyphens, apostrophes, etc... For instance, "MCDONALDS" would parse to "McDonalds"... the only problem is names like "de la Jose" or "von Mirkin" would parse out as "De La Jose" and "Von Mirkin". Even though this problem exist, it's a lot better than it was.

 

CREATE FUNCTION [dbo].[ParseNameString] (@text VARCHAR(50))
RETURNS VARCHAR(50)

BEGIN
 DECLARE @n INT
 SET @n = 1

 DECLARE @newstring VARCHAR(50)
 SET @newstring = ''
 
 SET @text = UPPER(LEFT(@text, 1)) + LOWER(RIGHT(@text, LEN(@text) - 1))

 -- start string building loop
 WHILE (@n < LEN(@text) + 1)
 BEGIN
  IF(SUBSTRING(@text, @n, 1) = 'M')
  BEGIN
   -- McXXXXX
   IF(SUBSTRING(@text, @n + 1, 1) = 'c')
   BEGIN
    
    SET @newstring = @newstring + 'Mc' + UPPER(SUBSTRING(@text, @n + 2, 1))
    SET @n = @n + 3
   END
   -- name MacXXXXX
   IF(SUBSTRING(@text, @n + 1, 1) = 'a')
   BEGIN
    IF(SUBSTRING(@text, @n + 2, 1) = 'c')
    BEGIN
     SET @newstring = @newstring + 'Mac' + UPPER(SUBSTRING(@text, @n + 3, 1))
     SET @n = @n + 4
    END
   END
  END
  -- apost. in the name (exmple: O'Sullivan)
  IF (SUBSTRING(@text, @n, 1) = '''')
  BEGIN
   SET @newstring = @newstring + SUBSTRING(@text, @n, 1) + UPPER(SUBSTRING(@text, @n + 1, 1))
   SET @n = @n + 1
  END
   ELSE
  -- space in the last name
  IF(SUBSTRING(@text, @n, 1) = ' ')
  BEGIN
   SET @newstring = @newstring + SUBSTRING(@text, @n, 1) + UPPER(SUBSTRING(@text, @n + 1, 1))
   SET @n = @n + 1
  END
   ELSE
  -- hyphen in the last name
  IF(SUBSTRING(@text, @n, 1) = '-')
  BEGIN
   SET @newstring = @newstring + SUBSTRING(@text, @n, 1) + UPPER(SUBSTRING(@text, @n + 1, 1))
   SET @n = @n + 1
  END
   ELSE
  BEGIN
   SET @newstring = @newstring + SUBSTRING(@text, @n, 1)
  END

  SET @n = @n + 1
 END  
 
 -- return the newly built string
 RETURN @newstring

END

Comments

Darrell said:

There's a proper case UDF (user-defined function) in the SQL Server 2000 resource kit. Also, you can check out an article on MSDN to see if they included something you did not: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro2k/html/sql00i11.asp
# June 17, 2003 5:07 PM

Jayme said:

From reading this article, I can find a UDF that allows you to capitalize the first character of a string, which is why it would not work for me. I needed to capitalize last names where appropriate - "McDonalds", for instance. This UDF would only capitalize the "M", and leave the rest lowercase unless I passed the character to capitalize. In this case, I would have to have another function to test for "what" to capitalize... so I guess the way I did it was the same amount of work as this method.

Thanks for your post - good read.

(found this on that site, pretty funny "I also wish that Microsoft would improve handling of parameters with default values." ... funny to see an article on MSDN saying "I wish that Microsoft")

Thanks again.
Jayme
# June 17, 2003 5:40 PM

Kael Dowdy said:

I can't think of how off the top of my head, but in T-SQL, can you get access to an Excel.Application COM/API so you can expose some of Excel's capabilities (like the PROPER() function)???

Just some thoughts.

Kael
# February 18, 2004 12:51 PM

Jason Finch said:

Just a quirk, this script dies if its passed a string of length 0..

Quick fix after the begin add

IF LEN(@text) = 0

RETURN ''

# July 2, 2007 4:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)