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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

CAST(X AS type)

Parameters

ParameterDescription
X The value.
type INTEGER, REAL, TEXT, NUMERIC or BLOB.

Return value

The converted value.

Examples

Convert typesTry it yourself
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
Text that isn't a numberTry it yourself
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 / 2 is 3 in SQLite because both values are integers. Cast one side (CAST(7 AS REAL) / 2) or write 7 / 2.0 to get 3.5.

In other languages

Was this page helpful?