sqlstreak

SQL error

column "age" is of type integer but expression is of type text

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

You're putting a text value into an integer column. Usually the columns are mismatched (the wrong column on the right-hand side, or INSERT … SELECT columns in a different order); when the text really holds numbers, convert it with CAST(x AS INTEGER).

The exact messages

  • PostgreSQLERROR: column "age" is of type integer but expression is of type text
  • SQLiteNo error, but a silent surprise: see below.

How to read this message

This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it. LINE gives the line it was reading when it gave up, and the ^ marks the exact spot. HINT is PostgreSQL's own suggestion, and it's usually worth following.

ERROR:  column "age" is of type integer but expression is of type text
LINE 1: UPDATE pets SET age = breed WHERE id = 1;
                              ^
HINT:  You will need to rewrite or cast the expression.

Why it happens

The wrong column on the right

SET age = breed copies text into a number column. Check which column you meant.

INSERT … SELECT in a different order

INSERT matches the SELECT's columns to the target columns by position. A text column landing in an integer position fails.

Numbers stored as text

An imported column of digits is text. CAST it (after cleaning) before writing it into a numeric column.

Reproduce it, then fix it

Both queries run on a real database in your browser. Switch dialects to compare.

Broken
UPDATE pets SET age = breed WHERE id = 1;
SELECT name, age FROM pets WHERE id = 1;
Fixed
UPDATE pets SET age = CAST('4' AS INTEGER) WHERE id = 1;
SELECT name, age FROM pets WHERE id = 1;

A number for a number column. When the source is text that holds digits, CAST makes the type explicit, and PostgreSQL accepts it.

The fix returns, PostgreSQL: 1 row

nameage
Biscuit4

The fix returns, SQLite: 1 row

nameage
Biscuit4

Experiment with it: PostgreSQL playground·SQLite playground

Other forms of this mistake

Each one was run when this page was built; the result is what the engine really does.

  • UPDATE pets SET age = breed WHERE id = 1;
    SELECT name, age, typeof(age) FROM pets WHERE id = 1;

    SQLiteRuns without an error

    SQLite stores 'Beagle' in the integer column: typeof says text.

What SQLite does instead

SQLite stores the text in the integer column without complaint (its types are only hints), so Beagle becomes a pet's age. The mistake only surfaces later, in sums and comparisons.

Official documentation

Stop hitting this error

Practise the concept behind it in a five-minute lesson, or read the full guide: SQL INSERT, UPDATE and DELETE.