Function – Proper case for names

Function to set names to the proper case. The first letter of each word is capitalized, the others are set to lower case. Won’t get every name right, but a good start.
create function dbo.ProperCase (
 @InputString nvarchar(2000)
)
returns nvarchar(2000)

as

begin
 
 declare @InputStringLength int
 declare @Counter int
 declare @CharacterCode int
 declare @Character nvarchar(1)
 declare @ReturnString nvarchar(2000)
 declare @UseUpper bit

 set @InputString = ltrim(@InputString)
 set @InputStringLength = len(@InputString)
 set @Counter = 0
 set @ReturnString = ”
 set @UseUpper = 1

 while (@Counter <= @InputStringLength)
  begin
   set @Counter = @Counter + 1
   set @Character = substring(@InputString, @Counter, 1)
   set @CharacterCode = ascii(@Character)

   if (@CharacterCode = 32)
    set @UseUpper = 1

   if (@CharacterCode between 65 and 90)
     begin
    if @UseUpper = 0
     set @Character = lower(@Character)
 
    set @UseUpper = 0
     end
   
   set @ReturnString = @ReturnString + @Character
  end

 return @ReturnString

end

go

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: