Convert Text to Sentence Case

Jimbo's picture

Harmonizing data often includes transforming names and addresses to UPPER CASE, but some clients opt for Sentence Case wherein only the first letter of each word is capitalized. Translating everything to upper case is very easy and now Sentence Case can, too.

This snippet is written for the scripting language that comes with BODS. It is akin to Java, but with cumbersome spelled out begin and end brackets.

Additionally, variables must be declared in the interface instead of in the code. The Return parameter that represents the output must be changed from its default of int to varchar of length 255 and a parameter called $input of type varchar must be added with a length of 255.

The code requires two local variables. They are $current of type int and $output of type varchar of length 255.

$output = upper( substr( $input, 1, 1) );
$current = 1;
while( $current < length( $input ) )
begin
  $current = $current +1;
  if( substr( $input, $current-1, 1) = ' ' or substr( $input, $current-1, 1) = '.' or substr( $input, $current-1, 1) = '/' )
    begin
      $output = $output || upper( substr( $input, $current, 1));
    end

  else
    begin
      $output = $output || lower( substr( $input, $current, 1));
    end
end

return $output;

Here is the same function in VBA for Microsoft Office products. The space, dash and forward slash characters here denote the beginning of a word and additional special characters can be added easily.

Function SentenceCase(strInput As String) As String

Dim strOutput As String
strOutput = UCase(Left(strInput, 1))

Dim nCurrent As Integer
nCurrent = 1
Do While nCurrent < Len(strInput)
    nCurrent = nCurrent + 1
    If InStr(1, " /-", Mid(strInput, nCurrent - 1, 1)) > 0 Then
        strOutput = strOutput & UCase(Mid(strInput, nCurrent, 1))
    Else
        strOutput = strOutput & LCase(Mid(strInput, nCurrent, 1))
    End If
Loop
SentenceCase = strOutput
End Function
Programming Language: 
VBA