# 8. Using Data Manipulation Functions
---[SIDEBAR]---
"type":"objectives",
"title":"Learning Objectives",
"text":"After completing this lesson you can:
- Describe the uses and limitations of data manipulation functions.
- Use functions to manipulate text.
- Use functions to manipulate dates and times.
- Use functions to manipulate numbers.
| Function | Argument(s) | Result |
|:--------:|------------------------------|-------------------------|
| UPPER | A string of text | Converts the text to all uppercase letters |
| LOWER | A string of text | Converts the text to all lowercase letters |
| LENGTH | A string of text | Returns the number of characters in the text |
| SUBSTR | A string of text, the starting position, and the number of characters to return | Returns the specified number of characters starting at the specified starting position in the text |
| INSTR | A string of text and a string of text to find within the first string | Returns the position number within the first string where the second string begins |
| LTRIM | A string of text | Removes spaces from left of the string |
| RTRIM | A string of text | Removes spaces from right of the string |
Suppose you want to view the full name of the artists all in uppercase letters. The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas|visible
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT UPPER(full_name) as full_name_uppercase
FROM artist;
---[SQL]---
Notice the syntax of the use of the UPPER function in the SELECT clause. The name of the function is UPPER. The argument for the UPPER function is a string of text. The values in the full_name column are text. The UPPER function will transform all of the text to uppercase.
For the next example, suppose you want to calculate the number of characters each artists' full name. Name the calculated column 'characters_in_name' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT full_name, LENGTH(full_name) as characters_in_name
FROM artist;
---[SQL]---
Notice the syntax of the use of the LENGTH function in the SELECT clause. The argument for the LENGTH function is a string of text. The values in the full_name column are text. The LENGTH function will calculate the number of characters in each artists' full name. Notice that, as expected, the number of characters is different for each artist.
In the next example you will need to use two functions. Suppose you want to extract the first name for each artists' from the full name column. Name the calculated column 'artist_first_name' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT full_name, LENGTH(full_name) as characters_in_name
FROM artist;
---[SQL]---
Notice the syntax of the use of the LENGTH function in the SELECT clause. The argument for the LENGTH function is a string of text. The values in the full_name column are text. The LENGTH function will calculate the number of characters in each artists' full name. Notice that, as expected, the number of characters is different for each artist.
In the next example assume you want to extract the first two characters of the artists' names. Name the calculated column 'artist_first_letters' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT full_name, SUBSTR(full_name, 1, 2) AS artist_first_letters
FROM artist;
---[SQL]---
The SUBSTR function is used to parse text by extracting a subset of text from another block of text. Notice the syntax of the SUBSTR function. It requires three arguments: the text to parse, where to start extracting text from the text, and how many characters to extract from the text. In this case, the text to parse is the values in the full_name column. You want to extract the first two characters, so you wish to start extracting at the first position. The final argument, the number of characters extract, is 2. The result of this function is the first two characters of each artists' name.
In the last example of text functions you will need to use two functions. Suppose you want to extract the first name for each artists from the full name column. You will need to use the SUBSTR and INSTR functions. Name the calculated column 'artist_first_name' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT full_name, SUBSTR(full_name, 1, INSTR(full_name, ' ') - 1) AS artist_first_name
FROM artist;
---[SQL]---
This is a significantly more complicated example. It demonstrates a couple of important elements of using functions. First, it demonstrates that a function result can be used as an argument for a different function. This is called "nesting" a function. Second, it also demonstrates that the result of a function may need to be adjusted to achieve the goal of the query.
The SUBSTR function is used to extract the first name of the artists from the full_name column. Recall that the SUBSTR function requires three arguments: the text to parse, the position of the first character to extract, and the number of characters to extract. In this example, the first name begins in the first character of the full_name column. Therefore, the first argument for the SUBSTR function is full_name and the second argument (where to start extracting the text) is 1 or the first position in the full_name column.
The number of characters to extract will be different for each artist, since the first name of each artist is different. The first name ends one character before the position of the first space (' ') in the full_name column for each artist. The INSTR function is used to find the location of the space character (' '). The first argument of the INSTR function is the text to search - in this case full_name. The second argument is the character to find which is the space (' '). The result of the INSTR function for each artist will be the location of the space character. This position must be adjusted by subtracting 1 character to reach the last character of the artists' first names. This is the third argument to the SUBSTR function (INSTR(full_name) - 1).
## Date and Time Manipulation Functions
Date and time functions are among the most useful SQL functions. Unfortunately, the various DBMS support different date and time functions. This is partly due to the fact that the different DBMS use their own formatting for dates and times. Common date and time functions are shown on the following table:
#### Commonly Used Date and Time Functions
| Function | Argument(s) | Result |
|:--------:|------------------------------|-------------------------|
| DATE | None | Returns the current date. |
| TIME | None | Returns the current time. |
| STRFTIME | optionally a date or time element to extract from a date/time value and a date/time value | Converts a date/value to text and optionally extracts a specified element of the date or time. |
The first example of using date and time functions is straightforward. Suppose you want to list the names of museums and the current date. Name the date column 'current_date' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT name, DATE() AS current_date
FROM museum;
---[SQL]---
This query lists the name of museums. It also includes the calculated field current_date which used the DATE() function to return the current date. Notice that the value for current_date is the same for each row of the query result. The NOW function works in a similar manner and returns the current time.
The next example is trickier. Suppose you want to list the names of museums and the year of the current date. Name the date column 'current_year' (without the quotes). You will use the STRFTIME function for this query. The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT name, STRFTIME('%Y', DATE()) AS current_year
FROM museum;
---[SQL]---
This query lists the name of museums. It also includes the calculated field current_year. The calculated field uses the STRFTIME() function. This function has two arguments. The first '%Y' instructs the function to return the year part of the date. The second argument is the result of the DATE() function which will return the current date. The calculated field will calculate the current year (the year part of the current date).
## Numeric Manipulation Functions
Numeric function are user to perform calculations on numbers that are more complicated than simple arithmetic. Unlike the text and date and time manipulation functions, these functions tend to be supported by most DBMS. The table below contains some example numeric functions.
#### Commonly Used Numeric Manipulation Functions
| Function | Argument(s) | Result
|:--------:|------------------------------|---------------|
| ABS() | A number | Returns the distance of the number from zero (absolute value) |
| SQRT() | A number | Returns the squared root of the number |
| PI() | None | Returns the value of PI |
Most numeric functions require numbers are arguments since they perform mathematical calculations. Some, like the PI function, return a known constant. Functions like PI are often used a part of a larger calculation. There are dozens of numeric functions implemented in most DBMS.
Suppose you want to calculate the approximate age of each artist at their death. This can be calculated as the difference between the birth and death value in the artist table. If you subtract the death year from the birth year, you will end up with a negative number. Use the ABS function to take the absolute value of this calculation. Name the calculated column 'age_at_death' (without the quotes). The syntax for a query that accomplishes this is:
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-sorted
---[SQL]---
SELECT full_name, ABS(birth - death) AS age_at_death
FROM artist;
---[SQL]---
This query demonstrates the use of the ABS function. Admittedly, you would not subtract the larger number death from birth to calculate the approximate age at birth. Notice that birth - death will produce a negative number. The ABS function calculates and returns the absolute value of each calculation.
## Summary
Functions are useful and can be used to perform calculations for each row of a query result and across the rows of a query result. This lesson described how to perform scalar calculations, or calculations on each row of the query results. It discussed how to use functions to manipulate text, dates and times, and numeric values. Unfortunately, not all DBMS implement these calculations the same way. Consult the documentation of your DBMS
to learn which functions are supported.