Technical Article

Convert String to Title Case

,

This function will evaluate an input string and convert it to title-case format. It uses the delimchars parameter to determine what are the triggering characters to indicate capitalization of the subsequent character.
I am using it in a DTS package to convert data in CAPS to Title/Proper case as I output the data to an Excel file.

/*This function will evaluate an input string and convert it to title-case format.
it uses the delimchars parameter to determine what are the triggering characters
 to indicate capitalization of the subsequent character*/
CREATE FUNCTION  fnTitleCase (@instring nvarchar(256))
RETURNS nvarchar(256)
AS

BEGIN
  DECLARE @strptr INT
  DECLARE @outstring nvarchar(255)
  DECLARE @strChar char(1)
  DECLARE @delimchars nvarchar(256)

  SET @outstring = ''
  SET @strptr = 0
  IF @delimchars is NULL
    /* Plug in typical upper-case delimiters
 NOTE: For localization purposes, you may wish to modify this */    SET @delimchars = ' ''-_.,'

  /* Loop through the entire string */  WHILE @strPtr < len(RTRIM(@instring))
    BEGIN
      SET @strptr = @strptr + 1
      SET @strchar = SUBSTRING(@instring,@strptr,1)
      IF @strptr = 1

 /* Assume that first character must always being upper-cased*/        SET @outstring = UPPER(@strchar)
      ELSE 

/*Check for other upper-case trigger character */        IF CHARINDEX(SUBSTRING(@instring,(@strptr - 1),1),@delimchars) > 0
--          SET @outstring = SUBSTRING(@outstring,1,@strptr)+UPPER(@strchar)
          SET @outstring = @outstring+UPPER(@strchar)
        ELSE
          SET @outstring = @outstring+LOWER(@strchar)
    END
   RETURN @outstring
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating