Skip to main content

SQL Server - Function to Convert String to Proper Case


Below is just a quick and simple function to convert a short string to proper case. It's not the most efficient but will do its job for short strings.

The first character of the string is converted to upper case, subsequent characters that are preceded by a space are converted to upper case.

e.g. "this IS A TEST sentence. followed BY ANOTHER."
will be changed to
"This Is A Test Sentence. Followed By Another."

Function:

CREATE FUNCTION [dbo].[fncProperCase](@string VarChar(1000))
RETURNS VarChar(1000)
AS
BEGIN

DECLARE @pos int
DECLARE @len int
DECLARE @char VarChar(1)
DECLARE @temp VarChar(1000)

SET @string = LOWER(LTRIM(RTRIM(@string)))
SET @temp = ''

SET @pos = 1
SET @len = LEN(@string)

WHILE @pos <= @len
BEGIN
SET @char = SUBSTRING(@string, @pos, 1)

IF @pos = 1
BEGIN
SET @temp = @temp + UPPER(@char)
END
ELSE
BEGIN

IF SUBSTRING(@string, @pos - 1, 1) = ' '
BEGIN
SET @temp = @temp + UPPER(@char)
END
ELSE
BEGIN
SET @temp = @temp + @char
END

END

SET @pos = @pos + 1
END

RETURN @temp
END

Example:

SELECT dbo.fncProperCase('this IS A TEST sentence. followed BY ANOTHER.')

Result:

This Is A Test Sentence. Followed By Another.

Comments

Popular posts from this blog

Which blog engine?

So the time has come to move to a more advanced blog engine for my blog. blogger.com , Google's blogging service, has served me well. It's incredibly easy to use and to get started with, along with having some great features such as inbuilt stats; however now I need a few more advanced features and greater control over the blog. There's a vast array of blog engines out there, some free, some paid for, some hosted, some self-hosted, and picking which one is best or the right choice could be a little bit tricky. This article from Mashable lists most of the main options and bigger players -  http://mashable.com/2007/08/06/free-blog-hosts/ . There are a few parameters that I've kind of decided on Ease of installation/compatibility and support with web hosts Simple to use. I don't want to spend ages clicking around just to add a post or format it. Feature rich and well supported. Most blog engines should have a fairly standard set of features now such RSS/ATOM fe...

Enable .NET 8 Preview in Visual Studio

Download the SDK using Download .NET 8.0 (Linux, macOS, and Windows) (microsoft.com)  and install it. To enable projects to target the .NET 8 preview framework, the preview option in Visual Studio needs to be enabled, otherwise the option to target .NET 8 will not be available as shown below when setting up a new project (or trying to upgrade an existing one). To allow .NET 8 Preview to be used as a target framework for projects, the preview option needs to be enabled in Visual Studio. Open Visual Studio and select "Continue without code" In Visual Studio, select Tools then Options In Options, under Environment, select Preview Features and enable Use previews of the .NET SDK.

EF CodeFirst Database.SetInitializser Requires Connection to 'master' Database Error

One of the features of EF CodeFirst is the ability to automatically drop and recreate a database if the model changes or if the database does not exist, which is pretty useful when just doing development work. In a web app this is done in the Global.asax file within "protected void Application_Start()" as per this post from Scott Guthrie -  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx . An example would be: protected void Application_Start() { Database.SetInitializer<yourdbcontex>(new DropCreateDatabaseIfModelChanges<yourdbcontext>()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } However when using the Database.SetInitializer and trying to rebuild the database, the following exception may be encountered, especially if using SQL Server Express: "This operation requires a connection to the 'master'...