Capitalize Function (Version 2.0) – T-SQL

Hi folks,

I’ve changed the t-sql code for Capitalize function, the previous capitalize function worked only if there is one space between words, but this version works even if there be more than one space between words.

with this version of Capitalize;

you can run the function like this:

select dbo.Capitalize(‘st    charles‘)

and this is a sample result:

St Charles

I used this code to replace multiple spaces with one space.

and the result is the Capitalize function that works on words or sentences and even if there are multiple spaces in the input.

CREATE FUNCTION [dbo].[Capitalize]


@text varchar(max)


RETURNS varchar(max)



declare @outputtext varchar(max)

declare @spaceCount int

declare @counter int

declare @term varchar(max)

declare @index int

declare @spaceIndex int

set @outputtext=”

set @text=ltrim(rtrim(@text))

while charindex(‘  ‘,@text)>0

set @text=replace(@text,’  ‘,’ ‘)

set @spaceCount= len(@text)-len(replace(@text,’ ‘,”))

set @counter=0

set @index=1

while @counter<=@spaceCount


if @index<>1 


set @index=@index+1


set @spaceIndex=charindex(‘ ‘,@text,@index)

if @spaceIndex=0


set @spaceIndex=len(@text)+1


if @index<>1


set @outputtext= @outputtext+’ ‘+ UPPER(substring(@text,@index,1))+LOWER(substring(@text,@index+1,(@spaceIndex-@index)-1))




set @outputtext= @outputtext+ UPPER(substring(@text,@index,1))+LOWER(substring(@text,@index+1,(@spaceIndex-@index)-1))


set @counter=@counter+1

set @index=@spaceIndex


— Return the result of the function

RETURN @outputtext


you can run the function like this:

select dbo.Capitalize(‘st    charles’)

and this is a sample result:

St Charles

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for nine continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog:
He wrote some books on MS SQL BI and also is writing some others, He was also an active member on online technical forums such as MSDN and Experts-Exchange, and was a moderator of MSDN SQL Server forums, and is an MCP, MCSE, and MCITP of BI. He is the leader of the New Zealand Business Intelligence users group. He is also the author of very popular book Power BI from Rookie to Rock Star, which is free with more than 1700 pages of content and the Power BI Pro Architecture published by Apress.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.

Leave a Reply