Hello Folks, you might be wondering for a function which would help you to convert your text input into a Title Case (or Proper Case) in SQL Server. Since T-SQL lacks a function to convert text to title case. So, it has to be User Defined.
Therefore, the following will surely accomplish you task:
CREATE FUNCTION udf_TitleCase (@a nvarchar(max)) RETURNS nvarchar(max) AS BEGIN DECLARE @b int SET @b = 1 SELECT @a = UPPER(SUBSTRING(@a,1,1))+LOWER(SUBSTRING(@a,2,LEN(@a)-1))+' ' WHILE @b < LEN(@a) BEGIN SELECT @b=CHARINDEX(' ',@a,@b) SELECT @a=SUBSTRING(@a,1,@b)+UPPER(SUBSTRING(@a,@b+1,1))+SUBSTRING(@a,@b+2,LEN(@a)-@b+1) SELECT @b=@b+1 END RETURN @a END
Now, we will test this function by giving an input:
SELECT dbo.udf_TitleCase('SQL sErVer gEEks iS a vEry gOOd coMMunity wEB sitE') AS TitleCase;
So, you can see the result below:
Hope you like it 🙂
Regards
Piyush Bajaj
Like us on FaceBook | Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook
Follow me on Twitter | Follow me on FaceBook