Home / SQL / Reference / Numbers and NULL

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

nhalfwhole_number_division
433.53

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.