Sub topic 8
Casting Values Between Types
Assume you know how to run SELECT statements and understand tables, columns, and constraints. This chapter shows how PostgreSQL converts a value from one data type to another.
Why casting matters
A data type controls which values a column can store and which operations PostgreSQL can perform. For example, PostgreSQL can add integers, but it cannot add arbitrary text values. Casting tells PostgreSQL how to interpret a value as another type.
PostgreSQL may perform some conversions implicitly. For predictable queries, especially at input boundaries, use an explicit cast.
Two forms of explicit casting
PostgreSQL supports the :: operator and the standard SQL CAST() function. They produce the same result.
SELECT
'42'::integer + 1 AS answer,
CAST('2026-01-15' AS date) AS order_date,
CAST(12.50 AS numeric(6, 2)) AS exact_amount;The :: form is concise and common in PostgreSQL code. The CAST() form is portable to more database systems and can be clearer in complex expressions.
You can cast column values as well:
SELECT
customer_id::text AS customer_id_text,
created_at::date AS created_date
FROM orders;Casting a timestamptz to date removes the time portion. The displayed date depends on the session time zone because PostgreSQL first interprets the instant in that zone.
Conversion errors
A cast fails when the source value cannot represent a valid value of the target type.
SELECT '42'::integer AS valid_number;
-- This statement raises an error:
-- SELECT 'abc'::integer;The error message identifies the invalid input syntax. Validate external text before inserting it, or handle conversion in application code when invalid input is expected.
A cast can also lose information. Converting a timestamp to a date removes its time. Converting a long decimal to an integer removes its fractional precision according to PostgreSQL's numeric conversion rules. Choose the target type deliberately.
NULL and implicit conversion
NULL means an unknown or missing value. Casting NULL produces NULL, not a default value.
SELECT NULL::integer AS missing_number;A cast does not replace missing data. Use COALESCE when you need a fallback:
SELECT COALESCE(NULL::integer, 0) AS number_with_default;PostgreSQL uses implicit casts only where the conversion is considered safe and unambiguous. It will not automatically convert every text expression to every other type. Operators and functions can therefore fail when their argument types do not match.
A practical casting pattern
Use a cast before applying type-specific operations:
SELECT
raw_value,
raw_value::integer * 100 AS cents
FROM import_rows
WHERE raw_value ~ '^[0-9]+$';The regular expression filters values containing only digits. The explicit cast then converts the remaining text to integers. This pattern prevents obvious conversion errors, but constraints should still protect the final table.
Exercise
What is the difference between value::date and CAST(value AS date) in PostgreSQL?
Show answer
They are two syntaxes for the same explicit conversion. The :: form is PostgreSQL-specific, while CAST() follows standard SQL syntax.