devxlogo

Title Case Function for SQL Server

As a SQL Server DBA, I realized that SQL Server has no function for Title Case, like the “initcap” function in Oracle. So I’ve made one that capitalizes the first letter of every word in a given text string:

create function initcap (@text varchar(4000))returns varchar(4000)asbegin	declare 	@counter int, 		@length int,		@char char(1),		@textnew varchar(4000)	set @text		= rtrim(@text)	set @text		= lower(@text)	set @length 	= len(@text)	set @counter 	= 1	set @text = upper(left(@text, 1) ) + right(@text, @length - 1) 	while @counter <> @length --+ 1	begin		select @char = substring(@text, @counter, 1)		IF @char = space(1)  or @char =  '_' or @char = ','  or @char = '.' or @char = '' or @char = '/' or @char = '(' or @char = ')'		begin			set @textnew = left(@text, @counter)  + upper(substring(@text, @counter+1, 1)) + right(@text, (@length - @counter) - 1)			set @text	 = @textnew		end		set @counter = @counter + 1	end	return @textend

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.