Convert with Casts
Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 1 of 22.
PostgreSQL distinguishes text from numbers. A cast asks it to convert a value into a specified type. Write CAST(value AS type) or PostgreSQL shorthand value::type.
SELECT CAST('14' AS integer) + 3 AS total;The text 14 becomes an integer before addition, so total is 17. Casting does not make invalid input valid: text such as fourteen cannot be converted to integer.
Use an explicit cast when an operation needs a different data type.
Challenge
MediumReturn one row with numeric_total equal to the numeric text 27 plus 8. Cast the text explicitly.
The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.
Try it yourself
-- Write your SQL here.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in SQL Foundations
1Types and Casts
Convert with CastsExact Decimal DivisionBoolean ValuesCreate a Typed TableActive Price ReviewPractice on your own: PostgreSQL playground