Click here to Skip to main content
Licence 
First Posted 15 May 2006
Views 49,521
Bookmarked 14 times

Capitalize the first character in SQL Server

By | 15 May 2006 | Article
This user-defined function will allow you to capitalize the first character of any string passed to it.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This is just a quick and very simple SQL Script you can use to capitalize the first letter of a string.  The best use for it is inline with your SQL Statement much like you use Left, Right, etc.

I needed a way to convert my existing usernames, that were all lower case with each part of the name separated by a period, into usable first and last names.  I could have done with in code, but I wanted to bind the results of my query to a drop down list.  Therefore, I wrote this script so I could have the formatted text returned, and easily bindable.

The script is designed to be a user-defined function so that it may be called easily from your statment.

Script

CREATE FUNCTION InitCap (
 @string varchar(255)
)  
RETURNS varchar(255) AS


BEGIN 

 RETURN upper(left(@string, 1)) + right(@string, len(@string) - 1) 

END

Examples

The following examples assums a table named tblCustomers with a user_name column.  Each username is formatted like erik.bartlow.

SELECT 
	InitCap(Left(user_name, CHARINDEX('.', user_name, 1) - 1)) as First_Name,
	InitCap(Right(user_name, (Len(id_user) - CHARINDEX('.', user_name, 1)))), as Last_Name,  	
	username,
FROM tblCustomers 
ORDER BY Last_Name

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Erik Bartlow

Web Developer

United States United States

Member

I currently reside in Spring, TX. I'm an avid programmer in C#, VB.NET, Javascript, ASP.NET. I also enjoy designing application icons.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTHIS IS RIGHT SAMPLE! Pinmember~Dim~20:48 4 Nov '10  
GeneralEmpty String PinmemberMember 417763710:37 3 Feb '10  
GeneralRe: Empty String PinmemberErik Bartlow13:11 3 Feb '10  
GeneralTrim the value PinmemberDon Pasquale22:24 22 Aug '07  
GeneralRe: Trim the value Pinmembernickml0078:54 10 Jun '10  
GeneralFunction will not work if passed in string is all upper PinmemberJon Ziehl18:28 9 Jan '07  
AnswerRe: Function will not work if passed in string is all upper PinmemberErik Bartlow4:50 10 Jan '07  
GeneralTip for a better article PinmemberJason Henderson7:59 15 May '06  
GeneralRe: Tip for a better article PinmemberErik Bartlow8:41 15 May '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 15 May 2006
Article Copyright 2006 by Erik Bartlow
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid