SQL CAST
Convert a value to another type.
Syntax
CAST(value AS type)
For example text to number, or a whole number to a decimal before dividing.
Example
Query (on the shop database used in the SQL course)
SELECT CAST('42' AS INTEGER) + 1 AS n, CAST(7 AS REAL) / 2 AS half, 7 / 2 AS whole_number_division;
Result
| n | half | whole_number_division |
|---|---|---|
| 43 | 3.5 | 3 |
1 row
Good to know. PostgreSQL also accepts the short form
value::type.Related
Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.