Engineering guides

Give CASE an explicit result type

KratosForge Editorial · 2026-11-25

Kratos and his wolf inspect measuring weights and a separate blank display plaque in a forge workshop.
Custom illustration · Keep the measurement and its display representation distinct.

A report needs a number when a score exists and a “missing” label when it does not. Mixing those values in one CASE can produce a conversion error, or a column whose type surprises the next reader. Even SELECT CASE WHEN 1=1 THEN '42' ELSE 0 END returns an integer in the local T-SQL implementation checked here. The quoted value in the selected branch does not make the result a text column.

T-SQL chooses the result type from the branch types using its precedence rules. An integer ranks above varchar, so text meeting an integer is converted toward the integer. In the pinned implementation, CASE WHEN 1=0 THEN 1 ELSE '2.5' END stops with a conversion error; it does not provide a decimal label. COALESCE and set operations can introduce the same integer/text boundary. Decide the output type deliberately instead of treating the chosen branch as a conversion policy.

The read-only example below keeps score numeric and creates a separate score_label. Both CASE results are text: the missing literal and an explicit conversion of the integer. VARCHAR(12) has room for this INT representation, including its sign. The VALUES fixture makes the example runnable without existing tables. It returns a numeric 42 alongside the text '42', and a NULL numeric score alongside the text 'missing'.

SQL · Read-only T-SQL fixture: retain the number and create a text label
WITH sample AS (
    SELECT * FROM (VALUES
        (1, CAST(42 AS INT)),
        (2, CAST(NULL AS INT))
    ) AS v(id, score)
)
SELECT id, score,
       CASE WHEN score IS NULL THEN 'missing'
            ELSE CAST(score AS VARCHAR(12))
       END AS score_label
FROM sample
ORDER BY id;

Use score for totals, numeric sorting and threshold comparisons. Use score_label for presentation. Sorting labels as text can put '10' before '2', and replacing a missing measurement with zero changes its meaning. If your input type changes to a decimal, a date or a wider number, choose an appropriate conversion and output width for that type rather than copying this integer-specific width.

Verify types as well as displayed values. A grid can make the integer 42 and text '42' look identical. For this fixture, the local result metadata reports id and score as int32 and score_label as string. Add zero, negative values, the allowed range boundaries and NULL to your own fixture, then check the actual downstream consumer. Matching a few visible cells does not establish the column contract.

The example and the two type/conversion checks were executed in KratosForge’s local T-SQL engine at main commit 711e0bc, using DuckDB 1.5.6. All 63 tests in the pinned T-SQL differential file passed locally; their expected rules are modelled from documentation, not a live warehouse run. This guide covers the integer/text case. The compatibility guide still lists decimal and float conversion limits. Check your installed build: Stable was last updated November 2 when checked on November 25.

← All publications

Search documentation

Start typing to search all 115 guides.

Search runs in your browser esc to close