4.2 Numeric & Mathematical Functions
Key Takeaways
ROUND(n [, d]) rounds to d decimal places; positive d rounds to the right of the decimal, d=0 or omitted rounds to the nearest integer, and negative d rounds to the left (tens, hundreds, etc.).
TRUNC(n [, d]) truncates digits without rounding; positive d truncates decimal places, d=0 or omitted removes all decimals, and negative d replaces digits to the left of the decimal point with zeros.
MOD(m, n) returns the remainder using the formula m - n * TRUNC(m/n), so the sign of the result always follows the dividend m; if n is 0, MOD returns m without error.
CEIL(n) returns the smallest integer greater than or equal to n, while FLOOR(n) returns the largest integer less than or equal to n, with critical directional differences on negative numbers.
SIGN(n) returns -1, 0, or 1 for negative, zero, and positive inputs respectively, while ABS(n) returns the positive magnitude.
4.2 Numeric & Mathematical Functions
Quick Answer: Numeric functions accept numeric inputs (
NUMBER,FLOAT,BINARY_FLOAT,BINARY_DOUBLE) and return numeric values. The most heavily tested functions on the 1Z0-071 exam areROUND,TRUNC,MOD,CEIL,FLOOR,ABS, andSIGN. Understanding the second precision parameterdinROUND(n, d)andTRUNC(n, d)—where negativedoperates on digits to the left of the decimal point—and knowing thatCEIL(-4.2)is-4whileFLOOR(-4.2)is-5are critical exam competencies.
The ROUND Function
The ROUND(n [, d]) function rounds n to d decimal places using standard mathematical half-way rounding (digits 5 through 9 round up away from zero; 0 through 4 round down).
Precision Parameter d Rules:
- Positive
d(d > 0): Rounds toddecimal places to the right of the decimal point. - Zero or Omitted
d(d = 0or omitted): Rounds to0decimal places (the nearest whole integer). - Negative
d(d < 0): Rounds todplaces to the left of the decimal point (tens, hundreds, thousands, etc.).
SELECT
ROUND(45.926, 2) AS r1, -- 45.93 (rounds to 2 decimal places)
ROUND(45.926, 0) AS r2, -- 46 (rounds to nearest whole integer)
ROUND(45.926) AS r3, -- 46 (default d is 0)
ROUND(45.926, -1) AS r4, -- 50 (rounds to nearest tens; 5 rounds up)
ROUND(44.926, -1) AS r5, -- 40 (rounds to nearest tens; 4 rounds down)
ROUND(55.926, -2) AS r6, -- 100 (rounds to nearest hundreds; 55 >= 50)
ROUND(45.926, -2) AS r7 -- 0 (rounds to nearest hundreds; 45 < 50)
FROM dual;
The TRUNC Function
The TRUNC(n [, d]) function truncates n to d decimal places without rounding (simply discarding discarded digits).
Precision Parameter d Rules:
- Positive
d(d > 0): Truncates toddecimal places to the right of the decimal point. - Zero or Omitted
d(d = 0or omitted): Discards all fractional digits, returning the whole integer part. - Negative
d(d < 0): Replacesddigits to the left of the decimal point with zeros.
SELECT
TRUNC(45.926, 2) AS t1, -- 45.92 (truncates at 2 decimal places)
TRUNC(45.926, 0) AS t2, -- 45 (truncates all decimal places)
TRUNC(45.926) AS t3, -- 45 (default d is 0)
TRUNC(45.926, -1) AS t4, -- 40 (truncates units digit to 0)
TRUNC(175.926, -2) AS t5, -- 100 (truncates tens and units digits to 0)
TRUNC(45.926, -2) AS t6 -- 0 (replaces tens and units with 0)
FROM dual;
Comprehensive Comparison: ROUND vs. TRUNC
The following table illustrates the exact behavior of ROUND and TRUNC across diverse positive, negative, and fractional inputs:
| Input Expression | Function Call | Result | Explanation |
|---|---|---|---|
156.784 | ROUND(156.784, 2) | 156.78 | 4 rounds down |
156.784 | TRUNC(156.784, 2) | 156.78 | Truncated after 2nd decimal place |
156.786 | ROUND(156.786, 2) | 156.79 | 6 rounds up |
156.786 | TRUNC(156.786, 2) | 156.78 | Truncated after 2nd decimal place |
156.786 | ROUND(156.786, 0) | 157 | 0.786 >= 0.5 rounds up |
156.786 | TRUNC(156.786, 0) | 156 | Fractional digits removed |
156.786 | ROUND(156.786, -1) | 160 | Units digit 6 >= 5 rounds up to 60 |
156.786 | TRUNC(156.786, -1) | 150 | Units digit 6 replaced with 0 |
156.786 | ROUND(156.786, -2) | 200 | Tens digit 5 >= 5 rounds up to 200 |
156.786 | TRUNC(156.786, -2) | 100 | Tens and units digits replaced with 0 |
156.786 | ROUND(156.786, -3) | 0 | Hundreds digit 1 < 5 rounds down to 0 |
-156.786 | ROUND(-156.786, 1) | -156.8 | 8 rounds away from zero to .8 |
-156.786 | TRUNC(-156.786, 1) | -156.7 | Truncated after 1st decimal place |
The MOD Function
The MOD(m, n) function returns the remainder of m divided by n.
Formal Mathematical Definition:
Oracle calculates MOD(m, n) using the formula:
Note that Oracle uses TRUNC, not FLOOR. This is exactly why Oracle's MOD diverges from the classical mathematical modulus whenever the two operands have opposite signs: MOD(-11, 4) returns -3 in Oracle, whereas a FLOOR-based modulus would return 1.
Key Rules for MOD:
- Sign Follows Dividend: The result of
MOD(m, n)has the same sign asm(the dividend). - Zero Divisor Rule: If
n = 0,MOD(m, 0)returnsm. It does not raise adivision by zeroerror (ORA-01476). - Decimal Support:
MODworks with fractional numbers as well as integers.
SELECT
MOD(11, 4) AS m1, -- 3 (11 - (4 * 2) = 3)
MOD(11, -4) AS m2, -- 3 (Sign follows dividend 11)
MOD(-11, 4) AS m3, -- -3 (Sign follows dividend -11)
MOD(-11, -4) AS m4, -- -3 (Sign follows dividend -11)
MOD(10.5, 3) AS m5, -- 1.5 (10.5 - (3 * 3) = 1.5)
MOD(25, 0) AS m6 -- 25 (If divisor is 0, returns dividend without error)
FROM dual;
Tip
A classic use of MOD in queries is testing for even/odd numbers: WHERE MOD(employee_id, 2) = 0 filters for even employee IDs.
CEIL and FLOOR Functions
CEIL and FLOOR map real numbers to adjacent integers.
CEIL(n): Returns the smallest integer greater than or equal ton("ceiling" / rounding up toward positive infinity).FLOOR(n): Returns the largest integer less than or equal ton("floor" / rounding down toward negative infinity).
SELECT
CEIL(2.1) AS c1, -- 3 (Smallest integer >= 2.1)
CEIL(2.0) AS c2, -- 2 (Smallest integer >= 2.0)
CEIL(-2.1) AS c3, -- -2 (Smallest integer >= -2.1; notice -2 is GREATER than -2.1)
FLOOR(2.9) AS f1, -- 2 (Largest integer <= 2.9)
FLOOR(2.0) AS f2, -- 2 (Largest integer <= 2.0)
FLOOR(-2.1) AS f3 -- -3 (Largest integer <= -2.1; notice -3 is LESS than -2.1)
FROM dual;
Negative Number Traps on the Exam:
CEIL(-4.8)is-4, NOT-5.FLOOR(-4.8)is-5, NOT-4.
ABS and SIGN Functions
ABS(n)
Returns the absolute value (magnitude) of a number n. Always returns a non-negative number.
SELECT ABS(-150.75) AS a1, ABS(150.75) AS a2, ABS(0) AS a3
FROM dual;
-- Returns: a1 = 150.75, a2 = 150.75, a3 = 0
SIGN(n)
Determines the sign of a number n. Returns:
-1if0if1if
SELECT SIGN(-450) AS s1, SIGN(0) AS s2, SIGN(892.5) AS s3
FROM dual;
-- Returns: s1 = -1, s2 = 0, s3 = 1
Summary of 1Z0-071 Numeric Function Traps
- Negative
din ROUND/TRUNC: Negative precision moves to the left of the decimal point.ROUND(58.2, -1)=60,TRUNC(58.2, -1)=50. - Omitted Precision: Omitting
dinROUND(n)orTRUNC(n)defaults tod = 0(nearest whole integer). - MOD Zero Divisor:
MOD(n, 0)returnsnand does NOT produceORA-01476: divisor is equal to zero. - MOD Sign Convention: The sign of
MOD(m, n)matches the sign ofm, irrespective ofn. - CEIL vs. FLOOR on Negative Numbers:
CEILmoves toward zero for negative numbers (CEIL(-1.2) = -1), whileFLOORmoves away from zero (FLOOR(-1.2) = -2).
What is the result of executing the following SQL statement?
SELECT ROUND(784.625, -2) + TRUNC(784.625, -2) AS total_val FROM dual;
1568
1500.625
1600
1500
Evaluate the following query in Oracle SQL:
SELECT MOD(29, -5) AS mod_val, MOD(14, 0) AS zero_div FROM dual;
What are the resulting values for mod_val and zero_div?
mod_val = 4, zero_div = 14
mod_val = -1, zero_div = 14
mod_val = 4, zero_div raises an ORA-01476: divisor is equal to zero error
mod_val = -4, zero_div = 0
What is the output of the following SQL query?
SELECT CEIL(-5.75) AS c_val, FLOOR(-5.75) AS f_val, SIGN(-5.75) AS s_val FROM dual;
c_val = -6, f_val = -5, s_val = -1
c_val = -5, f_val = -6, s_val = -1
c_val = -5, f_val = -5, s_val = 1
c_val = -6, f_val = -6, s_val = -1
Sections you finish are checked off in the contents.