Creating Formulas with Character Functions

Objective

After completing this lesson, you will be able to create formulas using character functions

Character String Functions

The following character string functions are used in this lesson:

FunctionDescription
Right()Extracts a specified number of characters from the right
NameOf()Returns the name of an object
Replace()Replaces a specified string with another string
Left()Extracts a specified number of characters from the left
SubStr()Extracts a specified string from within a string using variable length and position
Length()Identifies the length of a string
Pos()Identifies the position of a character in a string

Right() and Left() functions are only useful for manipulating constant strings or a constant number of characters. The SubStr() function is a more powerful function. Use the SubStr() function to extract strings where the length and position of the specified strings are not consistent.

Functions in Formulas and Variables

You use the functions of character strings to create a formula. You create a formula by typing it in the Formula field or in the Formula Editor. Either method calculates the data and displays the resulting values in the column that you have highlighted in the block.

You can also drag and drop objects in the Formula field and complete the formula by typing in the Formula field.

The table shows a comparison of the use of variables and formulas:

VariablesFormulas
Can be used in different blocks or reports in the documentOnly for use in a single cell
Can be identified as an objectN/A

If in doubt, always create a variable instead of a formula because there are certain things that can be done with variables that cannot be done with formulas.

Character Extraction Using the Right() Function

The Year object in the eFashion.unx universe is formatted to return the year as a four-digit value (for example, 2017). However, to display the year in a two-digit format (for example, 17), use the Right() character string function to display the last two characters.

Quarterly Sales by Year: See text before image for more details.

The Right() function displays a fixed number of characters, starting from the right of the character string, and removes the others.

The character string function Left() works in the same way, except that the function displays the specified number of characters starting from the left.

Syntax

Returns the rightmost characters of a string.

string Right(string;num_chars)

Example Syntax

In the example, we want to display only the last 2 characters, you use the following syntax:

=Right([Year];2)

The example syntax can be broken down into the following parts:

SyntaxDescription
[Year]Name of the object that contains the data
2Number of characters to display from the right of the character string

Section Header Modification

Modify the section header to include the Year name and modify it to display two digits using the following syntax:

=NameOf([Year])+" "+Right([Year];2)

The example syntax =NameOf([Year])+" "+Right([Year];2) can be broken down into the following elements:

SyntaxDescription
NameOf([Year])To display the name of the Year object.
+" "To concatenate a space after the object name.
+ Right([Year]);2)To concatenate and display two characters starting from the right end of the character string.
Using the NameOf() and Right() Functions to Modify the Display: See text before image for more details.

Characters Extracted Using the SubStr() Function

The Store name object in the eFashion.unx universe returns the name of each store in the company eFashion. However, each store name is prefixed by "e-Fashion".

Suppose that you want to remove the "e-Fashion" portion of the character string. Formatting the report to remove the "e-Fashion" prefix is a challenge because the length of each store name is different, with or without the "e-Fashion" portion. Neither the Right() or Left() functions works here.

You can remove the "e-Fashion" prefix by using the SubStr() function.

Store Names in the Company eFashion: See text before image for more details.

The SubStr() function extracts a specified character string from within a string.

Syntax

Returns part of a string.

string Substr(string;start;length)

Example Syntax

In the example, we want to display the store name without the "e-Fashion" prefix.

=SubStr([Store name];11;Length([Store name]))

The example syntax can be broken down into the following parts:

SyntaxDescription
[Store name]The string from which the substring is to be extracted.
11The position of the first character in the input string to be extracted.
Length({Store name])The number of characters to be extracted.

Using the SubStr() Function to Modify the Display

If you replace the Store name column with the Store column, the report displays the store names without the e-Fashion prefix.

Using the SubStr() Function: See text before image for more details.

String Replacement Using the Replace() Function

In the example, we want to substitute the letter "Q" for the word "Quarter".

Report on Quarterly Revenue: See text before image for more details.

The Replace() function will modify the character string displayed.

Syntax

Replaces part of a string with another string.

string Replace(replace_in;replaced_string;replace_with)

Example Syntax

You use the following syntax:

=Replace([Quarter];"Q";"Quarter ")

The example syntax can be broken down into the following parts:

SyntaxDescription
[Quarter]Describes the name of the object that retrieves the data.
"Q"Describes the value stored in the database followed by 1 through 4 to indicate the first through fourth quarters of the year.
"Quarter "Describes the character string that replaces "Q" in every instance.

(Insert a space between the word Quarter and the final quotation mark.)

If you have a report on quarterly revenue that you want to format to show the word "Quarter" instead of the letter "Q", use the Replace() function to modify the character string.

Using the Replace() Function to Modify the Display

The figure, Quarterly Revenue, shows the resulting report when you replace the Quarter column with the new variable.

Quarterly Revenue: See text before image for more details.

Character Position Using the Pos() Function

You are working on a report that contains the Store and Address columns.

In the Address column, you want to display only the street names for each of the eFashion stores. The Address object from the universe returns both the building number and the street name. The number of spaces before the street name begins is inconsistent. Therefore, you must create a substring to return only the portion of the address that is needed.

Notice that in each address, there is a space after the building number. If you can, create a formula that recognizes the position of the space for each entry. Use that position as a starting point for the SubStr() function.

Stores and Addresses: See text before image for more details.

Create a variable Space that contains the syntax:

=Pos([Address];" ")

Create a variable Address that contains the syntax:

=SubStr([Address];[Space]+1;Length([Address]))

Syntax

int Pos(test_string;pattern[;start][;end]])

Example Syntax

=Pos([Address] ; " ")

The example syntax can be broken down into the following parts:

SyntaxDescription
[Address]The string to be tested for the text pattern.
" "The text pattern.

Using the Pos() Function

The Space variable locates the first space within each address string. If you add a Space column to the report, the result displays the number of characters before the street name for each address, as shown in the figure, Using the Pos Function to Divide the Address Object.

The integer that the Space variable returns corresponds to the number of characters before the street address begins. You can now create a variable called Address that removes the building number from the address string.

To finish the report, delete the Space column and replace the Address column with the Address variable that you created.

Using the Pos() Function to Divide the Address Object: See text before image for more details.

Apply String Manipulation Formulas

Log in to track your progress & complete quizzes