Nulls are contagious. A missing middle initial should not wipe out a whole concatenated name. A zero in a “not applicable” column should not pull an average down. DB2 for z/OS gives you a small toolkit: COALESCE (and VALUE, IFNULL, NVL), NULLIF, DECODE, and CASE. This page shows what each one does, how they relate, and which form to write in new SQL.
Every function on this page is a convenience spelling of CASE. When the logic has ranges, multiple columns, or LIKE predicates, write searched CASE. Use the functions when the pattern is exactly “first non-null,” “null if equal,” or “lookup this code.”
123456-- Searched CASE CASE WHEN BONUS IS NULL THEN 0 WHEN BONUS < 0 THEN 0 ELSE BONUS END
Simple CASE compares one expression to a list of values. It does not treat two nulls as equal (unknown is not true). That difference is why DECODE exists for Oracle ports.
COALESCE returns the first non-null argument. Schema SYSIBM.
1COALESCE(expression, expression [, expression ...])
COALESCE(e1, e2) is the same as:
1CASE WHEN e1 IS NOT NULL THEN e1 ELSE e2 END
12345678910-- Missing salary displays as 0 SELECT EMPNO, COALESCE(SALARY, 0) AS SALARY FROM DSN8C10.EMP; -- Full outer join key that might be on either side SELECT COALESCE(S1993.DEPTNO, S1994.DEPTNO) AS DEPT, S1993.SALES, S1994.SALES FROM S1993 FULL JOIN S1994 ON S1993.DEPTNO = S1994.DEPTNO;
For names:
12345SELECT RTRIM(FIRSTNME) CONCAT ' ' CONCAT COALESCE(RTRIM(MIDINIT) CONCAT ' ', '') CONCAT RTRIM(LASTNAME) AS FULLNAME FROM DSN8C10.EMP;
| Name | Arguments | Notes |
|---|---|---|
| COALESCE | Two or more | Standard SQL; preferred name |
| VALUE | Two or more | Synonym for COALESCE |
| IFNULL | Exactly two | Identical to COALESCE with two arguments |
| NVL | Two (Oracle-style) | Same idea as IFNULL / two-argument COALESCE on z/OS |
12SELECT EMPNO, IFNULL(SALARY, 0) FROM DSN8C10.EMP;
Prefer COALESCE in new SQL even for two arguments. IFNULL and NVL show up in ported code and in people who learned another DBMS first.
NULLIF returns null if the two arguments are equal; otherwise it returns the first argument. Arguments must be compatible. Neither can be BLOB, CLOB, DBCLOB, or XML. Distinct types are allowed when compatible.
1NULLIF(expression1, expression2)
Equivalent CASE:
1CASE WHEN e1 = e2 THEN NULL ELSE e1 END
When e1 = e2 is unknown because one or both arguments is null, the CASE is not true, so NULLIF returns e1. That is easy to misread: NULLIF(NULL, 0) is null because e1 is null, not because of a match. NULLIF(0, NULL) is 0.
123456-- Sentinel 0 means "no bonus" for AVG SELECT AVG(NULLIF(BONUS, 0)) AS AVG_REAL_BONUS FROM DSN8C10.EMP; -- IBM example: 4500 + 500 equals 5000, so the result is null VALUES NULLIF(4500.00 + 500.00, 5000.00);
DECODE compares expression1 to each expression2. On match (including null-to-null) it returns the corresponding result-expression. If nothing matches, it returns else-expression, or null if you omit ELSE.
1234DECODE(expression1, expression2, result-expression [, expression2, result-expression ...] [, else-expression])
Differences from simple CASE:
1234567SELECT ID, DECODE(STATUS, 'A', 'Accepted', 'D', 'Denied', CAST(NULL AS CHAR(1)), 'Unknown', 'Other') AS STATUS_TEXT FROM CONTRACTS;
Equivalent searched CASE for the same null-aware map:
123456CASE WHEN STATUS = 'A' THEN 'Accepted' WHEN STATUS = 'D' THEN 'Denied' WHEN STATUS IS NULL THEN 'Unknown' ELSE 'Other' END
Shop standard on z/OS: write the CASE form in new programs. Keep DECODE when you are copying Oracle SQL and need identical null matching without rewriting every branch.
Combining COALESCE and NULLIF is a common “clean the column” pattern: COALESCE(NULLIF( RTRIM(col), ''), 'N/A') turns blanks and empty strings into N/A and leaves real text alone.
Indicator variables still matter. COALESCE(SALARY, 0) in SQL means the host PIC S9(7)V99 COMP-3 can be NOT NULL from the program’s point of view — you get 0 instead of a −1 indicator. That is often what you want for reports. For “was it really missing?” keep the indicator and skip COALESCE.
123456EXEC SQL SELECT EMPNO, COALESCE(SALARY, 0) INTO :EMPNO, :SALARY FROM DSN8C10.EMP WHERE EMPNO = :EMPNO END-EXEC.
COALESCE is a teacher asking kids in a line for a pencil until someone actually has one. VALUE, IFNULL, and NVL are nicknames for that teacher. NULLIF is a rule: “if these two answers are the same, pretend there is no answer.” DECODE is a sticker chart: this code gets this sticker, that code gets that sticker, and a missing code can still match a missing box on the chart. CASE is the full instruction sheet when the chart is not enough.
1. COALESCE(NULL, NULL, 5, 7) returns:
2. VALUE is:
3. NULLIF(10, 10) returns:
4. How does DECODE treat two nulls compared with simple CASE?
5. IFNULL compared with COALESCE: