SQL Server UDF Function Convert String to Proper Case / Title Case Example

04 Sep 2016 Kamal Pratap
0 Comments 2312 Views

In most cases when we require to display the text in Proper Case or Title Case using SQL Server. To handle this situation we explain the user defined function for convert the string in Title Case or Proper Case.

Create Function [dbo].[udf_TitleCase]
(@Text as varchar(8000))
returns varchar(8000)
   declare @Reset bit
   declare @Result varchar(8000)
   declare @i int
   declare @c char(1)

   select @Reset = 1, @i=1, @Result = ''
   while (@i <= len(@Text))
   	select @c= substring(@Text,@i,1),
               @Result = @Result + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
               @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
               @i = @i +1
   return @Result

Then run the following query in query analyzer

select dbo.udf_ProperCase('this function example return string into title case')


Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.


No coments found to display!

Leave a Comment