10/15/2012

SQL Server # TSQL to Convert STRING in PROPER format

Problem Statement

SQL Server has got in-built functions to convert the given string into LOWER() or UPPER() format but it does not provides any direct way to convert it to PROPER format. A string/text is said to be in a PROPER format if all the words in that string/text starts with a CAPITAL letter.

E.g. If a string is - “Hello, how are you?”,

String in Lower format = “hello, how are you?”

String in Upper format = “HELLO, HOW ARE YOU?”

and String in Proper format = “Hello, How Are You?”

 

Implementation

Ideally, SQL Server is not the right place to implement this kind of logic, as string operations are costlier in SQL from performance perspective. it should be either implemented in the Front-End language or the Reporting Tool as this more related to the formatting. However, if this is to be implemented in SQL, the more preferred way is to use SQL-CLR function. It does not mean that we can not achieve this with T-SQL.

Today, I will share a simple T-SQL function, which could be used to convert any given string in PROPER format. Below is the script -

CREATE FUNCTION [dbo].[PROPER]



(



  @StrToConvertToProper AS VARCHAR(MAX)



) 



RETURNS VARCHAR(MAX) 



AS



BEGIN



  --Trim the Text



  SET @StrToConvertToProper = LTRIM(RTRIM(@StrToConvertToProper))



 



  --Find the No. of Words in the Text



  DECLARE @WordCount AS INT



  SET @WordCount = LEN(@StrToConvertToProper) - LEN(REPLACE(@StrToConvertToProper,' ','')) + 1



 



  --Variable to track the space position



  DECLARE @LastSpacePosition AS INT = 0



 



  --Loop through all the words



  WHILE(@WordCount > 0)



    BEGIN



      --Set the Space Position



      SET @LastSpacePosition = CHARINDEX(' ',@StrToConvertToProper,@LastSpacePosition + 1)



      



      --Replace the Character



      SET @StrToConvertToProper = STUFF(@StrToConvertToProper,



                                        @LastSpacePosition + 1,



                                        1,



                                        UPPER(SUBSTRING(@StrToConvertToProper, @LastSpacePosition + 1, 1)))



      --Decrement the Loop counter                                      



      SET @WordCount = @WordCount - 1



    END



    



  RETURN @StrToConvertToProper



END  




When the above script is used as –





SELECT dbo.PROPER('hello, how are you?')






we get the following result - Hello, How Are You?



Conclusion



The given script could be used to convert any string in PROPER format using T-SQL. However, I would personally prefer converting the string at the Front-End or in the Reporting tool to display the string in this format.

1 comments:

  1. Hi

    This is very useful - thank you.

    However, this doesn't work if you try to convert from all UPPER CASE, however ot does work if you convert UPPER CASE to LOWER CASE first - is it supposed to or am I missing someting?

    Cheers

    Richard

    ReplyDelete

I appreciate your time, thanks for posting your comment. I will review and reply to your comment as soon as I can.

Thank you
Hemantgiri