PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42846: cannot cast type integer to date

There is no direct cast between the two types, so ::type cannot convert the value.

PostgreSQL 17.12
42846: cannot cast type integer to date

Jump to a cause

  1. 01Casting a yyyymmdd integer column to date
  2. 02ALTER COLUMN ... TYPE int USING date_column::int
  3. 03Counting true values with SUM(flag::numeric)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

PostgreSQL only allows value::type when a cast between the two types exists. Some pairs that look natural, such as integer to date, date to integer, or boolean to numeric, have no cast, and the query fails with 42846 cannot cast type X to Y. The caret under LINE 1 points at the cast.

The fix is to go through a function or a type that does have a cast. The three runs below are the cases seen most in real code.

01Casting a yyyymmdd integer column to date

sales.sale_ymd stored dates as integers like 20240105. SELECT sale_ymd::date FROM sales failed with cannot cast type integer to date.

information_schema.columns confirmed the column is integer. Converting to text and parsing it, to_date(sale_ymd::text, 'YYYYMMDD'), returned 2024-01-05 and 2024-02-20.

Setup
Setuprun as lab_a
CREATE TABLE sales (id int, sale_ymd int, amount int);
INSERT INTO sales VALUES (1, 20240105, 100), (2, 20240220, 70);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT id, sale_ymd::date AS sale_date FROM sales;
✕ ERROR
ERROR:  42846: cannot cast type integer to date
LINE 1: SELECT id, sale_ymd::date AS sale_date FROM sales;
                           ^
Checkrun as lab_a
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'sales' AND column_name = 'sale_ymd';
OUTPUT
 column_name | data_type
-------------+-----------
 sale_ymd    | integer
(1 row)
After the fixrun as lab_a
SELECT id, to_date(sale_ymd::text, 'YYYYMMDD') AS sale_date FROM sales ORDER BY id;
✓ SUCCESS
 id | sale_date
----+------------
  1 | 2024-01-05
  2 | 2024-02-20
(2 rows)

02ALTER COLUMN ... TYPE int USING date_column::int

Changing orders.order_date from date to int with USING order_date::int failed with cannot cast type date to integer.

Formatting the date first, USING to_char(order_date, 'YYYYMMDD')::int, worked. The column became integer with values 20240105 and 20240220.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int, order_date date);
INSERT INTO orders VALUES (1, DATE '2024-01-05'), (2, DATE '2024-02-20');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
ALTER TABLE orders ALTER COLUMN order_date TYPE int USING order_date::int;
✕ ERROR
ERROR:  42846: cannot cast type date to integer
LINE 1: ...ders ALTER COLUMN order_date TYPE int USING order_date::int;
                                                                 ^
Fixrun as lab_a
ALTER TABLE orders ALTER COLUMN order_date TYPE int USING to_char(order_date, 'YYYYMMDD')::int;
OUTPUT
ALTER TABLE
After the fixrun as lab_a
SELECT id, order_date, pg_typeof(order_date) FROM orders ORDER BY id;
✓ SUCCESS
 id | order_date | pg_typeof
----+------------+-----------
  1 |   20240105 | integer
  2 |   20240220 | integer
(2 rows)

03Counting true values with SUM(flag::numeric)

SELECT SUM(is_paid::numeric) FROM invoices failed with cannot cast type boolean to numeric.

The column was boolean. boolean can be cast to int, so SUM(is_paid::int) returned 2. COUNT(*) FILTER (WHERE is_paid) also returned 2.

Setup
Setuprun as lab_a
CREATE TABLE invoices (id int, is_paid boolean);
INSERT INTO invoices VALUES (1, true), (2, false), (3, true);
OUTPUT
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
SELECT SUM(is_paid::numeric) AS paid_count FROM invoices;
✕ ERROR
ERROR:  42846: cannot cast type boolean to numeric
LINE 1: SELECT SUM(is_paid::numeric) AS paid_count FROM invoices;
                          ^
Checkrun as lab_a
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'invoices' AND column_name = 'is_paid';
OUTPUT
 column_name | data_type
-------------+-----------
 is_paid     | boolean
(1 row)
After the fixrun as lab_a
SELECT SUM(is_paid::int) AS paid_count, COUNT(*) FILTER (WHERE is_paid) AS paid_count2 FROM invoices;
✓ SUCCESS
 paid_count | paid_count2
------------+-------------
          2 |           2
(1 row)

FAQ

How do I convert an integer like 20240105 to a date in PostgreSQL?

Go through text: to_date(col::text, 'YYYYMMDD'). A direct col::date gives 42846.

How do I change a date column to integer?

Use a USING expression that formats the date: ALTER TABLE t ALTER COLUMN d TYPE int USING to_char(d, 'YYYYMMDD')::int;.

How do I count true values of a boolean column?

SUM(flag::int) or COUNT(*) FILTER (WHERE flag). Both returned 2 in the run above; flag::numeric fails with 42846.

What is the difference between 42846 and 42804?

42846 means no cast exists between the two types. 42804 (datatype mismatch) is raised when a value of one type is used where another type is required without an explicit cast.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12