sqlstreak

SQL function

CAST in SQL: convert types in PostgreSQL and SQLite

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

Short answer

CAST(value AS type) converts a value to another type: CAST('42' AS INTEGER) is the number 42. PostgreSQL refuses anything it can't convert exactly; SQLite converts as much as it can and keeps going, which matters for messy text and for dates.

Syntax and availability

PostgreSQLSQLite
AvailableYes, plus the :: shorthandYes (no ::)
SyntaxCAST(value AS type) / value::typeCAST(value AS type)

Examples

Budget text into numbers

Strip the $ and the spaces first; blanks become NULL so the cast doesn't trip on them.

SELECT id, budget AS raw,
       CAST(NULLIF(REPLACE(TRIM(budget), '$', ''), '') AS INTEGER) AS budget_usd
FROM adoption_requests
ORDER BY id
LIMIT 6;

Result (PostgreSQL and SQLite, identical): 6 rows

idrawbudget_usd
1$120120
28080
3$150150
4NULLNULL
5$6060
6$9595

Try it: PostgreSQL playground · SQLite playground

Avoid integer division

SELECT category, COUNT(*) AS items,
       CAST(COUNT(*) AS REAL) / (SELECT COUNT(*) FROM menu_items) AS share
FROM menu_items
GROUP BY category
ORDER BY items DESC;

Result (PostgreSQL and SQLite, identical): 4 rows

categoryitemsshare
pizza50.45454545454545453
side30.2727272727272727
drink20.18181818181818182
dessert10.09090909090909091

Try it: PostgreSQL playground · SQLite playground

Numbers into text

SELECT name, 'Item #' || CAST(id AS TEXT) AS label
FROM menu_items
ORDER BY id
LIMIT 4;

Result (PostgreSQL and SQLite, identical): 4 rows

namelabel
MargheritaItem #1
Pepperoni RocketItem #2
Four Cheese OrbitItem #3
BBQ MeteorItem #4

Try it: PostgreSQL playground · SQLite playground

How it behaves: edge cases, measured

Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.

CasePostgreSQLSQLite
Clean number textCAST('42' AS INTEGER)42CAST('42' AS INTEGER)42
Number followed by junkCAST('42abc' AS INTEGER)error: invalid input syntax for type integer: "42abc"CAST('42abc' AS INTEGER)42
Decimal text to integerCAST('3.99' AS INTEGER)error: invalid input syntax for type integer: "3.99"CAST('3.99' AS INTEGER)3
Decimal number to integerCAST(3.99 AS INTEGER)4CAST(3.99 AS INTEGER)3
Date text to DATECAST('2026-03-15' AS DATE)'2026-03-15'CAST('2026-03-15' AS DATE)2026
Text with no number at allCAST('abc' AS REAL)error: invalid input syntax for type real: "abc"CAST('abc' AS REAL)0
1 to booleanCAST(1 AS BOOLEAN)trueCAST(1 AS BOOLEAN)1
NULLCAST(NULL AS INTEGER)NULLCAST(NULL AS INTEGER)NULL

Common mistakes

  • CAST(d AS DATE) in SQLite: there is no date type, so it converts the text to a number (2026). Keep ISO text and use the date functions.

  • Expecting the same rounding: casting 3.99 to INTEGER rounds in PostgreSQL and truncates in SQLite (see the matrix).

  • Casting messy text in PostgreSQL without cleaning it: one bad row stops the whole query.

PostgreSQL and SQLite

The same syntax with very different temperaments, all measured in the matrix: PostgreSQL rounds decimals and rejects junk; SQLite truncates, reads leading digits, turns unreadable text into 0 and has no real DATE or BOOLEAN type.

Official documentation

Go deeper

Make it stick in five minutes a day

“Tidy text and numbers” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free