T-SQL Function for Capitalize Word and Sentences

Hi folks,

Today I faced a situation that I need to capitalize sentences in one column of a SQL Server database table, so I created a function to do that, I put this here to share with you.

this function will change sentences like  :

select dbo.Capitalize(‘sample text for capitalization!‘)

to :

Sample Text For Capitalization!

Here you will find the function :

CREATE FUNCTION dbo.Capitalize

(

@text varchar(max)

)

RETURNS varchar(max)

AS

BEGIN

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))


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

set @counter=0

set @index=1

while @counter<=@spaceCount

begin


if @index<>1 

begin

set @index=@index+1

end 


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


if @spaceIndex=0

begin

set @spaceIndex=len(@text)+1

end

if @index<>1

begin

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

end

else

begin

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

end


set @counter=@counter+1

set @index=@spaceIndex


end


RETURN @outputtext


END

GO

and here you can see how to use this function:

select dbo.Capitalize(‘sample text for capitalization!’)

and result will be like this:

Sample Text For Capitalization!

This function has still spaces for development, for example if you have more than single space between words this will break, I will update it in future appropriate times.

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: https://radacad.com/blog.

Leave a Reply