Almost every SELECT list eventually adds a raise, subtracts a discount, or glues two names together. DB2 expressions use a small set of arithmetic operators (+, -, *, /, and their unary forms), plus concatenation and a family of bit functions. This page walks through each operator, precedence, nulls, integer division, and the bit operations you use instead of C-style & and |.
If an expression uses arithmetic operators, the result is the value you get by applying those operators to the operands. Operators can be written in infix form (a + b) or, for some operators, as functions ("+"(a, b)). Arithmetic applies to signed numeric types and to datetime values in the addition and subtraction cases IBM documents. USER + 2 is invalid: you cannot add an integer to a string. Distinct numeric types need sourced functions before + and * work.
If any operand is NULL, the result of the arithmetic expression is NULL. That is why salary + commission without COALESCE drops rows (or yields null results) when commission is unknown. Division by zero is an error for ordinary numeric types (decimal floating-point has its own special-value rules).
| Operator | Meaning |
|---|---|
| + (infix) | Addition of numbers or datetime ± duration |
| - (infix) | Subtraction of numbers or datetime − datetime/duration |
| * | Multiplication (numeric only) |
| / | Division (numeric only; divisor must not be zero) |
| + (unary) | Unary plus; operand unchanged |
| - (unary) | Unary minus; reverses sign |
| CONCAT or || | String concatenation |
| BITAND / BITOR / … | Bitwise functions, not infix operators |
Infix + adds two numeric operands, or adds a labeled duration to a datetime value (for example DATE + 1 MONTH). The result type follows numeric type combination rules: two integers yield an integer (BIGINT if either operand is BIGINT); mixing DECIMAL and INTEGER yields a decimal with computed precision and scale.
123SELECT SALARY + COALESCE(COMM, 0) AS PAY, HIREDATE + 6 MONTHS AS SIX_MONTHS FROM HR.EMPLOYEE;
Datetime addition is not the same as adding integers to a CHAR date. Use DATE/TIMESTAMP types (or CAST) and labeled durations (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MICROSECOND) rather than string tricks.
Infix - subtracts numbers, subtracts a duration from a datetime, or subtracts two datetime values to produce a duration (the exact result type depends on the operands—date minus date is a number of days in common teaching examples; check the SQL Reference for timestamp differences). Multiplication and division must not be applied to datetime values.
123SELECT SALARY - 500, CURRENT DATE - HIREDATE AS DAYS_EMPLOYED FROM HR.EMPLOYEE;
* multiplies numeric operands. Overflow follows the result type: two large INTEGERs can overflow INTEGER; DECIMAL overflow is a different SQLCODE. Scale of a decimal product is the sum of scales (subject to product precision limits). Use CAST to DECIMAL with an explicit scale when you care about pennies.
123SELECT SALARY * 1.03 AS NEXT_YEAR, QTY * UNIT_PRICE AS LINE_AMT FROM ORD.LINE;
/ divides numeric operands. The divisor must not be zero. If both operands are integers, Db2 performs binary integer division and discards the remainder: 5 / 2 is 2, not 2.5. If you need a fractional result, CAST an operand to DECIMAL or DECFLOAT first.
12VALUES 5 / 2; -- 2 (integer division) VALUES DECIMAL(5,5,1) / 2; -- 2.5 style decimal result
Decimal division scale is influenced by the precompiler DEC option, DECARTH, MINDVSCL, and DECDIV3 subsystem parameters, and by CURRENT PRECISION for dynamic SQL. If shop reports show unexpected rounding, those settings—not the / token—are the first place to look.
Unary + (prefix plus) does not change its operand. It is rarely useful except to make a sign explicit.
Unary - reverses the sign of a nonzero, non-DECFLOAT operand. For DECFLOAT it reverses the sign of all values, including zero and special values (NaNs and infinities). If the type of A is SMALLINT, the type of -A is INTEGER (large integer). The token after a prefix operator must not begin with another plus or minus—write - (A) or -A, not -- which starts a comment in many SQL dialects or is simply invalid as a double prefix.
123VALUES + SALARY; VALUES - COMM; VALUES - SMALLINT_COL; -- result type INTEGER
When parentheses do not say otherwise:
Operators at the same level run left to right. People remember school math for * vs + and then forget that CONCAT sits with * and /, so a mix of concatenation and addition should always be parenthesised for readers even when the engine is sure.
123VALUES 2 + 3 * 4; -- 14 VALUES (2 + 3) * 4; -- 20 VALUES 10 - 2 - 3; -- (10 - 2) - 3 = 5
Concatenation is not numeric arithmetic, but it is an expression operator with the same precedence band as * and /. CONCAT and || both join two compatible strings into one. Operands must be compatible strings: binary cannot concatenate with character, including CHAR FOR BIT DATA. Distinct types based on strings need a sourced CONCAT (or an overloaded "||" function).
12345SELECT FIRSTNME CONCAT ' ' CONCAT LASTNAME AS FULL_NAME FROM HR.EMPLOYEE; -- Equivalent, but CONCAT is the portable spelling: -- FIRSTNME || ' ' || LASTNAME
Vertical bars (or the substitute characters some national code pages use) can parse incorrectly after CCSID conversion when a statement travels between systems. IBM recommends CONCAT for that reason. If either operand is null, the concatenation result is null—not an empty string. Length of the result follows documented MIN/MAX rules (including mixed CCSID conversion expanding bytes).
Db2 for z/OS does not give you C’s &, |, and ^ as SQL infix operators. Bit work is done with scalar functions that operate on the two’s complement representation of SMALLINT, INTEGER, BIGINT, or DECFLOAT (DECIMAL, REAL, and DOUBLE arguments are cast to DECFLOAT and truncated to whole numbers):
123456789-- Turn on property bit 16 UPDATE ITEM SET PROPERTIES = BITOR(PROPERTIES, 16) WHERE ITEMID = 3412; -- Toggle bit 1024 UPDATE ITEM SET PROPERTIES = BITXOR(PROPERTIES, 1024) WHERE ITEMID = 3412;
Results come back as base-10 integers of a type derived from the arguments—not as binary strings. For raw byte strings use BINARY/BLOB functions and SUBSTR, not BITAND.
123456SELECT EMPNO, LASTNME CONCAT ', ' CONCAT FIRSTNME AS NAME, SALARY * 12 AS YEARLY, BITOR(FLAGS, 1) AS FLAGS_WITH_ACTIVE FROM HR.EMPLOYEE WHERE SALARY + COALESCE(COMM, 0) > 50000;
Arithmetic in the WHERE clause is an expression; the comparison > is a different operator family (next page). Keep computations in the SELECT list when you need to display them, and be aware that wrapping a column in SALARY * 12 can block index use on SALARY.
Plus, minus, times, and divide are the same buttons as on a school calculator. Times and divide happen before plus and minus unless you put up fence posts (parentheses). If a box is empty (NULL), the calculator gives up and the answer is empty too. CONCAT is taping two word-stickers into one longer sticker. Bit functions are flipping light switches on a panel of on/off flags inside a number—not taping words and not adding pocket money.
Infix + (add), - (subtract), * (multiply), and / (divide), plus unary + and unary -. Datetime values support addition and subtraction with labeled durations, not multiplication or division. If any operand is null, the result is null.
Parentheses first. Then unary + and -. Then multiplication, division, and concatenation (CONCAT or ||). Then addition and subtraction. Same-level operators evaluate left to right. Use parentheses whenever a human might misread the expression.
CONCAT or || joins two compatible strings. Binary strings cannot concatenate with character strings. A null operand makes the result null. Distinct string types need a sourced CONCAT function. Prefer the CONCAT keyword over || for CCSID portability.
Not as C-like & and | tokens in Db2 for z/OS. Use the scalar functions BITAND, BITANDNOT, BITOR, BITXOR, and BITNOT on integer or DECFLOAT values. They operate on two’s complement bits and return a base-10 integer.
If A is small integer, the data type of -A is large integer (INTEGER). Unary plus does not change the operand. Unary minus on DECFLOAT also flips the sign of zero, NaN, and infinity special values.
1. What is 2 + 3 * 4 in Db2 arithmetic?
2. What happens if any operand of + is NULL?
3. Why is CONCAT often preferred over || ?
4. Integer division 5 / 2 yields:
5. How do you set bits in an INTEGER column in Db2 for z/OS?