SQLite 3 reference · Conditional and Type Functions
SQLite CAST Expression
Convert a value to another type: CAST(X AS INTEGER), REAL, TEXT, NUMERIC or BLOB. Text that isn't a number becomes 0.
SQLite 3
Syntax
CAST(X AS type)
Parameters
| Parameter | Description |
|---|---|
X |
The value. |
type |
INTEGER, REAL, TEXT, NUMERIC or BLOB. |
Return value
The converted value.
Examples
SELECT CAST('42' AS INTEGER) + 1 AS a, CAST(7 AS REAL) / 2 AS b, CAST(3.99 AS INTEGER) AS c;
Output
a | b | c 43 | 3.5 | 3
SELECT CAST('12abc' AS INTEGER) AS leading_digits, CAST('abc' AS INTEGER) AS no_digits;
Output
leading_digits | no_digits 12 | 0
Common mistakes
7 / 2is3in SQLite because both values are integers. Cast one side (CAST(7 AS REAL) / 2) or write7 / 2.0to get3.5.
In other languages
CAST and ::· PostgreSQL
Related
TYPEOF()· Return the storage class of a value: 'null', 'integer', 'real', 'text' or 'blob'. Shows how SQLite stored it.ROUND()· Round a number to a given number of decimal places (0 by default). Halves round away from zero.- SQL Aggregate Functions · tutorial