Sunday, 25 March 2018

Convert first character of each word in UPPER case in sql server

Recently I worked on the report. My requirement is to display the customer name first character in upper case. SQL Server doesn't provide any built-in function for this requirement. In this article, I am creating T-SQL function. This function after every space, first later will be in UPPER case.
Example:
Input: BAGESH KUMAR SINGH
The output will be: Bagesh Kumar Singh
Below is the function
CREATE FUNCTION [dbo].[Convert_Upper_Case_First_Char]
(@Str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
  DECLARE @Result varchar(2000)
  SET @Str = LOWER(@Str) + ' '
  SET @Result = ''
  WHILE 1=1
  BEGIN
    IF PATINDEX('% %',@Str) = 0 BREAK
    SET @Result = @Result + UPPER(Left(@Str,1))+
    SubString  (@Str,2,CharIndex(' ',@Str)-1)
    SET @Str = SubString(@Str,
      CharIndex(' ',@Str)+1,Len(@Str))
  END
  SET @Result = Left(@Result,Len(@Result))
  RETURN @Result
END

See below.


Function created successfully.
Let’s use this function.




No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts