Proper Case in Sql Server

Here is the Sql function that will Proper Case any column in a table

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
create function [dbo].[ProperCase](@Text as varchar(8000))
returns varchar(8000)
as
begin
declare @Reset bit;
declare @Ret varchar(8000);
declare @i int;
declare @c char(1);
select @Reset = 1, @i=1, @Ret = ”
while (@i <= len(@Text))
select @c= substring(@Text,@i,1),
@Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
@Reset = case when @c like ‘[a-zA-Z]‘ or @c in (””) then 0 else 1 end,
@i = @i +1
return @Ret
end

–Example

declare @name varchar(100)
set @name =’marcus ford’
set @name = dbo.propercase(@name)
print @name

Result — Marcus Ford

3 Responses to “Proper Case in Sql Server”

  1. Bruce Willis says:

    You Spelt My Name Wrong.
    But Don’t Worry, The Function Is Very Handy.

    ~mf

  2. Great post! Just wanted to let you know you have a new subscriber- me!

Leave a Response