Numeric scalar functions in DB2 for z/OS take a number (or a string that can be cast to a number) and return a number. They round, chop, take remainders, raise to powers, draw pseudo-random values, and compute logs. This page is the map of ABS, CEILING/CEIL, FLOOR, MOD, POWER, RAND, ROUND, SIGN, SQRT, TRUNCATE, EXP, LN, and LOG10 — plus a short note on two names that are not numeric at all (XMLNAMESPACES and EXPLAIN).
These functions are scalar: one input row’s arguments produce one result. If any argument is null, the result is null. A character or graphic string argument is typically implicitly cast to DECFLOAT(34) before the math runs, so '12.5' can be FLOOR'd, but 'N/A' fails conversion.
Result types usually match the input numeric type, with documented exceptions (FLOOR of DECIMAL uses scale 0; some mixed expressions promote to DECFLOAT or DOUBLE). Overflow and invalid operations raise SQL errors for BINARY INTEGER / DECIMAL / FLOAT; DECFLOAT can yield special values (NaN, infinity) instead of always failing.
Do not confuse these with conversion functions named INTEGER, DECIMAL, or DOUBLE — those change type. ROUND(salary, 0) is still the original type with a rounded value. INTEGER(salary) is a 4-byte integer, truncating toward zero.
ABS(numeric-expression) returns the absolute value: negative numbers lose their sign, zero and positives stay as they are. ABSVAL is a synonym. The result has the same type (and for DECIMAL, the same precision and scale) as the argument.
123456SELECT ABS(-18.7) AS A1, ABS(0) AS A0, SIGN(-18.7) AS SNEG, SIGN(0) AS SZER, SIGN(18.7) AS SPOS FROM SYSIBM.SYSDUMMY1;
SIGN returns −1, 0, or +1 according to the sign of the argument. It is the cheap way to implement “direction” in report SQL (for example SIGN(actual - budget) as over/under) without a three-way CASE. For DECFLOAT, signed zeros and NaN have extra rules in the SQL Reference; if you live in DECFLOAT, read SIGN’s DECFLOAT notes before assuming ordinary −1/0/+1.
These four all “remove fractional noise,” but they aim at different integers.
| Expression | Result | Direction |
|---|---|---|
| CEILING(3.1) / FLOOR(3.1) | 4 / 3 | Toward +∞ / toward −∞ |
| CEILING(-3.1) / FLOOR(-3.1) | -3 / -4 | Still toward +∞ / toward −∞ |
| TRUNCATE(3.1, 0) / TRUNCATE(-3.1, 0) | 3 / -3 | Toward zero; fractional digits dropped |
| ROUND(1.5, 0) / ROUND(-1.5, 0) | 2 / -2 | Half away from zero (half-up) |
| ROUND(748.58, 1) / ROUND(748.58, -2) | 748.6 / 700 | Positive places = right of decimal; negative = left |
Smallest integer value greater than or equal to the argument. CEILING(3.1) is 4. CEILING(−3.1) is −3. Positive numbers go up; negative numbers go toward zero when the fraction is nonzero? No: −3.1 toward +infinity is −3. Remember “toward +infinity” and the table above.
Largest integer value less than or equal to the argument. FLOOR(3.1) is 3. FLOOR(−3.1) is −4. IBM’s sample on DSN8C10.EMP: FLOOR(MAX(SALARY)/12) walks Christine Haas’s 52750 annual salary down to 4395 from 4395.83.
123SELECT FLOOR(3.5), FLOOR(3.1), FLOOR(-3.1), FLOOR(-3.5) FROM SYSIBM.SYSDUMMY1; -- 3, 3, -4, -4
TRUNCATE(numeric-expression-1, numeric-expression-2) chops digits without rounding. The second argument defaults to 0. Positive 2 means keep two digits to the right of the decimal. Negative 2 means chop to hundreds (two places to the left). Truncation is toward zero: TRUNCATE(−1.9, 0) is −1, unlike FLOOR(−1.9) which is −2.
ROUND(numeric-expression-1, numeric-expression-2) rounds to a number of places. The second argument defaults to 0. Behaviour is ROUND_HALF_UP: a 5 rounds away from zero. ROUND does not use the DECFLOAT ROUNDING MODE special register. If you need that register’s mode (half-even, down, ceiling, and the rest), use QUANTIZE on DECFLOAT values.
12345SELECT ROUND(748.58, 0) AS R0, -- 749 ROUND(748.58, 1) AS R1, -- 748.6 ROUND(748.58, -2) AS RNEG, -- 700 TRUNCATE(748.58, 0) AS T0 -- 748 FROM SYSIBM.SYSDUMMY1;
Negative places are the usual way to round money to thousands for executive summaries. Document the second argument in comments; a missing second argument silently means 0 and will change a DECIMAL(9,2) salary into a whole-dollar figure in the result value (type may still show scale, depending on type rules — check the result in SPUFI).
MOD(numeric-expression-1, numeric-expression-2) returns the remainder of the first argument divided by the second. If the second argument is zero, the operation is invalid. For integers, the result is integer. For DECIMAL, scale rules follow the reference (the result scale relates to the first argument).
1234SELECT MOD(9, 4) AS M1, -- 1 MOD(9, -4) AS M2, MOD(YEAR(CURRENT DATE), 2) AS ODD_EVEN_YEAR FROM SYSIBM.SYSDUMMY1;
MOD is the usual “every nth row” helper when combined with a dense key, and the usual “is this year even?” helper. It is not a substitute for bitwise AND; use BITAND for flags. Sign of the remainder follows IBM’s rules for the operand types — verify with your actual types if you port formulas from another DBMS that uses a different remainder sign convention.
These are the algebraic and transcendental functions. Arguments that cannot convert to a numeric type fail. Domain errors (log of zero or negative, square root of negative on non-DECFLOAT) fail.
123456SELECT POWER(2, 10) AS KIB, SQRT(2) AS ROOT2, EXP(1) AS E_APPROX, LN(10) AS LN10, LOG10(1000) AS ORDERS FROM SYSIBM.SYSDUMMY1;
Trigonometric cousins (SIN, COS, TAN, and the hyperbolic set) exist too but are not in this page’s list. They take radians, not degrees; DEGREES and RADIANS convert.
RAND() or RANDOM() returns a DOUBLE in the interval 0 ≤ value < 1. RAND(integer) seeds the generator. The same seed in the same environment produces a repeatable sequence, which is what you want in a test case and not what you want if you thought “seed 1” meant “more random.”
1234SELECT RAND() AS R1, RAND() AS R2, RAND(5) AS SEEDED FROM SYSIBM.SYSDUMMY1;
RAND is non-deterministic. It restricts optimization and is banned in some contexts (for example certain generated columns and materialized query tables that require determinism). For a shuffle of employees, ORDER BY RAND() is a known pattern; it sorts the full result and does not scale. Prefer a keyed sample for large tables.
Topic lists sometimes park XMLNAMESPACES and “explain functions” beside ABS. They do not round numbers.
When you need both math and XML in one query, keep the numeric functions on numeric columns and the XML constructors on XML columns. Mixing them in one expression is usually a type error.
1234567SELECT EMPNO, SALARY, ROUND(SALARY, -3) AS SAL_THOUSANDS, FLOOR(SALARY / 12) AS MONTHLY_FLOOR, MOD(INTEGER(EMPNO), 2) AS ODD_EMPNO FROM DSN8C10.EMP WHERE ABS(SALARY - 50000) < 5000;
Wrapping SALARY in ROUND in the WHERE clause can prevent index matching on SALARY. Compute a persisted rounded column if you filter on it often. In the SELECT list, rounding is display logic and is cheap compared with a tablespace scan.
ABS takes the minus sign off a number, like turning around a backward toy car so it faces forward. FLOOR is “go down the stairs to the next whole step.” CEILING is “go up to the next whole step.” TRUNCATE snaps off the extra Lego studs without moving to a different step. ROUND looks at the leftover studs and decides whether to climb one more step; a leftover of 5 or more climbs. MOD is the leftover after you share bricks into equal piles. RAND is shaking a bag of numbered tickets and pulling one fraction between zero and one. SQRT asks “which pile size, times itself, makes this pile?”
1. What is FLOOR(-3.1)?
2. How do CEILING and FLOOR differ for 3.1?
3. What rounding mode does ROUND use?
4. What does RAND() return?
5. What is SIGN(-18.7)?