Sub topic 10
Handling Values That Cannot Be Converted
Prerequisites: You should know basic SELECT and WHERE queries and be familiar with PostgreSQL types such as integer, numeric, date, and timestamp.
A cast parses one value as another type. If the input is not valid for the target type, PostgreSQL raises an error and aborts the statement. For example:
SELECT 'abc'::integer;This produces an error similar to:
invalid input syntax for type integer: "abc"The same applies to values outside a type's range:
SELECT '999999999999999999999'::integer;The text is numeric, but it cannot fit in PostgreSQL's 32-bit integer type.
Distinguish invalid syntax from invalid meaning
Different target types reject different inputs. A value can have the right general shape but still be invalid.
| Input | Target type | Result |
|---|---|---|
| `'42'` | `integer` | Valid |
| `'3.7'` | `integer` | Invalid integer syntax |
| `'2026-11-27'` | `date` | Valid |
| `'2026-99-99'` | `date` | Invalid date |
| `'true'` | `boolean` | Valid |
| `'maybe'` | `boolean` | Invalid Boolean input |
A failed cast normally aborts the whole statement. It does not automatically turn the bad value into NULL.
Validate before casting
For simple formats, use a condition inside CASE. This example accepts only an optional sign followed by one or more digits:
WITH source(raw_value) AS (
VALUES ('42'), ('-7'), ('3.7'), ('unknown'), (NULL)
)
SELECT raw_value,
CASE
WHEN raw_value ~ '^[+-]?[0-9]+$'
THEN raw_value::integer
ELSE NULL
END AS parsed_value
FROM source;The regular expression checks the text before the cast runs. Invalid rows become NULL, so the query can continue.
NULLIF is useful for known special cases such as empty strings, but it does not protect against every malformed value:
SELECT NULLIF(trim(raw_value), '')::integer
FROM imported_values;This handles an empty or whitespace-only value. It still raises an error for a nonempty value such as 'abc'. Use a format check as well when the input is untrusted.
Test input safely in PostgreSQL 16+
PostgreSQL 16 introduced pg_input_is_valid. It tests whether text can be read as a specified type and returns a Boolean instead of raising an input error.
SELECT pg_input_is_valid('42', 'integer') AS valid_integer,
pg_input_is_valid('abc', 'integer') AS invalid_integer;The result is true and false. You can combine the function with a conditional cast:
WITH source(raw_value) AS (
VALUES ('42'), ('abc'), ('100000000000')
)
SELECT raw_value,
CASE
WHEN pg_input_is_valid(raw_value, 'integer')
THEN raw_value::integer
ELSE NULL
END AS parsed_value
FROM source;This checks both syntax and the target type's range. The second argument is the type name as text.
Choose an error policy
For imports, decide what should happen to rejected rows:
- Reject the entire batch when every value must be valid.
- Convert invalid values to
NULLwhen missing data is acceptable. - Store the original text and an error reason for later correction.
- Validate at the ingestion boundary so invalid data does not reach permanent tables.
Exercise
What happens when SELECT '3.7'::integer; runs, and how could you avoid aborting a query that contains this value?
Show answer
It raises an invalid input syntax error because text is parsed directly as an integer. Validate the value first with a regular expression or pg_input_is_valid on PostgreSQL 16 and later, then cast only when the check succeeds.