Important alert: (current site time 7/15/2013 7:50:18 PM EDT)
 

VB icon

Capitalize First Letter

Email
Submitted on: 4/18/2003 5:09:35 PM
By: Adonis Villanueva  
Level: Intermediate
User Rating: By 1 Users
Compatibility: SQL Server 2000
Views: 19801
 
     This is a User Defined Function (UDF) that will capitalize the first letter of a character after a space. It will also lowercase all capitalized words. This will only capitalize the first letter after the any space. Example: 'A CAT RAN DOWN' --> 'A Cat Ran Down' or 'a cat ran down' --> 'A Cat Ran Down'. Usage Select CapFirst('String')
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Capitalize First Letter
-- Description:This is a User Defined Function (UDF) that will capitalize the first letter of a character after a space. It will also lowercase all capitalized words. This will only capitalize the first letter after the any space. Example: 'A CAT RAN DOWN' --> 'A Cat Ran Down' or 'a cat ran down' --> 'A Cat Ran Down'. Usage Select CapFirst('String')
-- By: Adonis Villanueva
--
-- Inputs:String input
--
-- Returns:Formatted String
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=663&lngWId=5--for details.--**************************************

CREATE function dbo.CapFirst (@String varchar(255))
returns varchar(255)
as
begin --begin function procedure
declare @StringCount int
set @string = lower(@string)
set @string = stuff(@string,1,1,left(upper(@string),1)) --Capitalize the first letter 
set @StringCount = 0
while @StringCount < len(@string)
begin --begin while
 if substring(@string,charindex(space(1),@string,@StringCount),1) = space(1)
 begin --begin if	
set @string = stuff(@string,charindex(space(1),@string,@StringCount)+1,1,substring(upper(@string),charindex(' ',@string,@StringCount)+1,1)) 
 end --end if
set @StringCount = @StringCount + 1
end --end while
return @string --return the formatted string
end --end function procedure


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
9/13/2007 10:52:35 PMJohn

This script has an error if the string ends in a space, it will return NULL incorrectly. To resolve, change first @string set to "SET @string = lower(rtrim(ltrim(@string)))". Also, to not return a NULL you can use "RETURN ISNULL(@string,'')" instead of just returning the string.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.