Columns, literals, and host variables do not always share the same data type. When DB2 for z/OS combines them, it may convert values—either quietly (implicit conversion and promotion) or because you asked with CAST. This page ties together implicit conversion, promotion, casting, function resolution, and the SQL path.
SQL is strongly typed but pragmatic. Comparing an INTEGER column to a DECIMAL literal, assigning a CHAR value into VARCHAR, or calling a function that expects DOUBLE all require a well-defined type change. Db2 documents which pairs are compatible, which are castable, and which promotions are allowed. When a conversion is not allowed, you get an error instead of a silent wrong answer—usually what you want.
Three ideas work together:
Implicit conversion appears during assignment, comparison of compatible types, and some numeric↔string situations documented for your Db2 version. Examples of the spirit of the rules:
12345-- Compatible numerics compare after appropriate conversion/promotion WHERE SMALLINT_COL > 1000 -- Explicit is clearer when types look “stringly” WHERE INTEGER_COL = CAST('42' AS INTEGER);
Do not rely on implicit string↔numeric conversion in application SQL if you can avoid it. Explicit CAST documents intent, fails loudly on bad data, and makes reviews easier. Distinct types rarely convert implicitly to their source type—you usually must cast.
Promotion follows a best-to-worst precedence list within related groups. The “best” choice is the same type; worse choices are wider or more general related types. Db2 consults promotion when resolving functions, casting distinct types in some cases, and assigning built-in types to distinct types.
| From | Can promote toward |
|---|---|
| SMALLINT | INTEGER, BIGINT, decimal, real, double, DECFLOAT |
| INTEGER | BIGINT, decimal, real, double, DECFLOAT |
| CHAR / GRAPHIC | VARCHAR / VARGRAPHIC, then CLOB / DBCLOB |
| BINARY | VARBINARY, then BLOB |
Important nuance: DECFLOAT is not promoted “down” to binary floating-point for function resolution in the way smaller floats promote up—DECFLOAT’s precision and exponent range exceed classic float. Distinct types promote only to the same distinct type.
123-- SMALLINT argument can promote when resolving a function -- that expects INTEGER or DECIMAL VALUES POWER(SMALLINT_COL, 2);
Use the CAST specification when you want a definite target type:
1234SELECT CAST(SALARY AS DECIMAL(10,2)) AS SAL_DEC, CAST(HIREDATE AS CHAR(10)) AS HIRE_CHAR, CAST(NULL AS INTEGER) AS NULL_INT FROM HR.EMPLOYEE;
You can also call cast-oriented scalar functions (INTEGER, DECIMAL, CHAR, DATE, and many others). XML data uses XMLCAST in XML contexts. Truncating non-blank characters when casting to a shorter string typically raises a warning; truncating binary casts can raise an error.
When you CREATE TYPE for a distinct type, Db2 generates cast functions between the distinct type and its source type. You generally cannot compare a distinct-typed column directly to a built-in literal without casting one side so the types match.
1234-- Conceptual pattern WHERE PRICE > US_DOLLAR(100.00) -- or WHERE CAST(PRICE AS DECIMAL(9,2)) > 100.00;
Function resolution is Db2’s process for choosing which function to run when you write a name and argument list. It considers:
If two candidates fit equally well, you can get an ambiguous function error—qualify the name or cast arguments to steer resolution. Built-in functions in SYSIBM usually win when system schemas sit first on the path.
The SQL path is an ordered list of schemas. For dynamic SQL, CURRENT PATH supplies it (SET PATH / SET CURRENT PATH). For static SQL, the bind-time PATH option does. If you omit SYSIBM, SYSFUN, SYSPROC, and SYSIBMADM, Db2 still assumes them at the front in a defined order.
12SET PATH = SYSTEM PATH, HR, PAYROLL; VALUES MYFUNC(1); -- search path for unqualified MYFUNC
Changing CURRENT SCHEMA does not automatically put that schema on the path. After SET CURRENT SCHEMA = 'HR', still include HR on the path if you need unqualified UDFs in HR to resolve.
Imagine toy blocks of different shapes: small number blocks, bigger number blocks, and letter blocks. Sometimes Db2 quietly swaps a small number block for a bigger one so it fits a game (promotion / implicit conversion). Sometimes you say out loud “pretend this letter card is a number” with CAST. Function resolution is picking which board game to play when several games share a name. The SQL path is the shelf order Db2 searches when looking for that game.
1. What does CAST do?
2. What is data type promotion?
3. Can CHAR be promoted to VARCHAR?
4. What is function resolution?
5. Does the SQL path affect unqualified function names?