Home > Articles

Using Functions

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Composite Functions

An important characteristic of functions, whether they are character, mathematical, or date/time, is that two or more functions can be combined to create composite functions. A composite function with two functions can be said to be a function of a function. Let’s go back to the George Washington query to illustrate. Again, we’re working from this data:

032tab01.jpg

Remember that the President column is 20 characters long. In other words, there are three spaces to the right of the value “George Washington”. In addition to illustrating composite functions, this next example will also cover the RTRIM function mentioned in the previous section. The statement:

SELECT
RIGHT(RTRIM (President),10) AS 'Last Name'
FROM table1

returns this data:

032tab02.jpg

Why does this now produce the correct value? Let’s examine how this composite function works. There are two functions involved: RIGHT and RTRIM. When evaluating composite functions, you always start from the inside and work your way out. In this example, the innermost function is:

RTRIM(President)

This function takes the value in the President column and eliminates all spaces on the right. After this is done, the RIGHT function is applied to the result to bring back the desired value. Because

RTRIM(President)

equals “George Washington”, we can say that:

SELECT
RIGHT(RTRIM (President), 10)

is the same as saying:

SELECT
RIGHT('George Washington', 10)

In other words, we can obtain the desired result by first applying the RTRIM function to the input data and then adding the RIGHT function to the expression to produce the final results.

  • + Share This
  • 🔖 Save To Your Account