Retrieving Data with String and Arithmatic Expressions

In this third part of a nine-part series on retrieving data from tables with the SELECT statement, you will learn how to code both string and arithmetic expressions. This article is excerpted from chapter three of the book Murach’s Oracle SQL and PL/SQL, written by Joel Murach (Murach Publishing; ISBN: 9781890774509).

How to code string expressions

A string expression consists of a combination of one or more character columns and literal values. To combine, or concatenate, the columns and values, you use the concatenation operator (||). This is illustrated by the examples in figure 3-5.

The first example shows how to concatenate the vendor_city and vendor_state columns in the Vendors table. Notice that because no alias is assigned to this column, Oracle assigns a name, which is the entire expression. Also notice that the data in the vendor_state column appears immediately after the data in the vendor_city column in the results. That’s because of the way vendor_city is defined in the database. Because it’s defined as a variable-length column (the VARCHAR2 data type), only the actual data in the column is included in the result. In contrast, if the column had been defined with a fixed length, any spaces after the name would have been included in the result. You’ll learn about data types and how they affect the data in your result set in chapter 8.

The second example shows how to format a string expression by adding spaces and punctuation. Here, the vendor_city column is concatenated with a string literal, or string constant, that contains a comma and a space. Then, the vendor_state column is concatenated with that result, followed by a string literal that contains a single space and the vendor_zip_code column.

Occasionally, you may need to include a single quotation mark or an apostrophe within a literal string. If you simply type a single quote, however, the system will misinterpret it as the end of the literal string. As a result, you must code two single quotation marks in a row. This is illustrated by the third example in this figure.

How to concatenate string data

SELECT vendor_city, vendor_state, vendor_city || vendor_state
FROM vendors

How to format string data using literal values

SELECT vendor_name,
vendor_city || ‘, ‘ || vendor_state || ‘ ‘ ||
vendor_zip_code AS address
FROM vendors

How to include apostrophes in literal values

SELECT vendor_name || ”’s address: ‘,
vendor_city || ‘, ‘ || vendor_state || ‘ ‘ || vendor_zip_code
FROM vendors


  1. A string expression can consist of one or more character columns, one or more literal values, or a combination of character columns and literal values.
  2. The columns specified in a string expression must contain string data (that means they’re defined with the CHAR or VARCHAR2 data type).
  3. The literal values in a string expression also contain string data, so they can be called string literals or string constants. To create a literal value, enclose one or more characters within single quotation marks ().
  4. You can use the concatenation operator (||) to combine columns and literals in a string expression.
  5. You can include a single quote within a literal value by coding two single quotation marks, as shown in the third example above.

——————————————–Figure 3-5 How to code string expressions

{mospagebreak title=How to code arithmetic expressions}

Figure 3-6 shows how to code arithmetic expressions. To start, it summarizes the four arithmetic operators you can use in this type of expression. Then, it presents two examples that show how to use these operators.

The SELECT statement in the first example includes an arithmetic expression that calculates the balance due for an invoice. This expression subtracts the payment_total and credit_total columns from the invoice_total column. The resulting column is given the name balance_due.

When Oracle evaluates an arithmetic expression, it performs the operations from left to right based on the order of precedence. This order says that multiplication and division are done first, followed by addition and subtraction. If that’s not what you want, you can use parentheses to specify how you want an expression evaluated. Then, the expressions in the innermost sets of parentheses are evaluated first, followed by the expressions in outer sets of parentheses. Within each set of parentheses, the expression is evaluated from left to right in the order of precedence.

To illustrate how parentheses and the order of precedence affect the evaluation of an expression, consider the second example in this figure. Here, the expressions in the second and third columns both use the same operators. However, when Oracle evaluates the expression in the second column, it performs the multiplication operation before the addition operation because multiplication comes before addition in the order of precedence. In contrast, when Oracle evaluates the expression in the third column, it performs the addition operation first because it’s enclosed in parentheses. As you can see in the result set, these two expressions result in different values.

Unlike some other databases, Oracle doesn’t provide a modulo operator that can be used to return the remainder of a division operation. Instead, you must use the MOD function as described in the next figure.

The arithmetic operators in order of precedence

A SELECT statement that calculates the balance due

SELECT invoice_total, payment_total, credit_total,
invoice_total – payment_total – credit_total AS balance_due
FROM invoices

A SELECT statement that uses parentheses to control the sequence of operations

SELECT invoice_id,
invoice_id + 7 * 3 AS order_of_precedence,
(invoice_id + 7) * 3 AS add_first
FROM invoices
ORDER BY invoice_id


  1. Unless parentheses are used, the operations in an expression take place from left to right in the order of precedence. For arithmetic expressions, multiplication and division are done first, followed by addition and subtraction.
  2. Whenever necessary, you can use parentheses to clarify or override the sequence of operations. Then, the operations in the innermost sets of parentheses are done first, followed by the operations in the next sets, and so on.

——————————————–Figure 3-6 How to code arithmetic expressions

Please check back for the continuation of this series.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye