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 12 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, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
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.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog:

Leave a Reply