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
| PostgreSQL | SQLite | |
|---|---|---|
| Available | Yes, plus the :: shorthand | Yes (no ::) |
| Syntax | CAST(value AS type) / value::type | CAST(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
| id | raw | budget_usd |
|---|---|---|
| 1 | $120 | 120 |
| 2 | 80 | 80 |
| 3 | $150 | 150 |
| 4 | NULL | NULL |
| 5 | $60 | 60 |
| 6 | $95 | 95 |
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
| category | items | share |
|---|---|---|
| pizza | 5 | 0.45454545454545453 |
| side | 3 | 0.2727272727272727 |
| drink | 2 | 0.18181818181818182 |
| dessert | 1 | 0.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
| name | label |
|---|---|
| Margherita | Item #1 |
| Pepperoni Rocket | Item #2 |
| Four Cheese Orbit | Item #3 |
| BBQ Meteor | Item #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.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Clean number text | CAST('42' AS INTEGER)42 | CAST('42' AS INTEGER)42 |
| Number followed by junk | CAST('42abc' AS INTEGER)error: invalid input syntax for type integer: "42abc" | CAST('42abc' AS INTEGER)42 |
| Decimal text to integer | CAST('3.99' AS INTEGER)error: invalid input syntax for type integer: "3.99" | CAST('3.99' AS INTEGER)3 |
| Decimal number to integer | CAST(3.99 AS INTEGER)4 | CAST(3.99 AS INTEGER)3 |
| Date text to DATE | CAST('2026-03-15' AS DATE)'2026-03-15' | CAST('2026-03-15' AS DATE)2026 |
| Text with no number at all | CAST('abc' AS REAL)error: invalid input syntax for type real: "abc" | CAST('abc' AS REAL)0 |
| 1 to boolean | CAST(1 AS BOOLEAN)true | CAST(1 AS BOOLEAN)1 |
| NULL | CAST(NULL AS INTEGER)NULL | CAST(NULL AS INTEGER)NULL |
Related
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
- Guide: Data cleaning in SQL
- Practice: SQL data cleaning practice: 6 exercises with answers
- Lesson: Tidy text and numbers (Pro)
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.