Interpret the effect of the number functions TO_CHAR, ROUND, and TRUNC.
TO_CHAR, ROUND, TRUNC
Number Functions: TO_CHAR, ROUND, TRUNC
Oracle has added a whole range of mathematical functions that you can use with numbers. The table below lists most of them.
ACOS, ASIN, ATAN
Arc cosine, arc sine, and arc tangent
Arc tangent of col1 and col2
Smallest integer greater than or equal to col1
COS, SIN, TAN
Cosine, sine, tangent
COSH, SINH, TANH
Hyperbolic cosine, hyperbolic sine, and hyperbolic tangent
Constant e raised to col1 power. E=2.71828183 ...
Largest integer less than or equal to col1
Logarithm in basen of col1
Remainder of col1 divided by col2
Col1 raised to powern
Col1 is rounded to placen decimal places. Round to tens using -1 as placen.
Sign of col1; returns -1, 0, or +1.
Square root of col1
Convert col1 to a character string.
Truncate col1 to placen decimal places.
The next subsections illustrate a few of the more commonly used number functions.
Sometimes you need to concatenate a number to some character field.
To do so, you must first convert the number field into an equivalent character string. Use TO_CHAR to do this as shown in the example below.
Rounding can be useful when you have calculated data that must be rounded before storing in the database. For example, if you calculate shipping and handling as 10 percent of the sale, you might end up with a tenth of a cent in your calculation.
To adjust this, use the ROUND function. When you insert or update number data, Oracle accepts data with too many decimal places and automatically rounds the data to the correct number of decimal places. Oracle does not give you an error message, so you may not realize what happened until you display the stored data later.
Truncating and rounding are similar functions and use similar parameters. The difference, as you know, is how numbers are handled.
The following SlideShow shows a query that lists a numbers with variations on the ROUND function and the TRUNC function.
Round Trunc Functions
In the next lesson, you will learn how to identify valid date-related date functions and Oracle's standard date format.