Lesson 3 of 13 · 18 min
CAST and data types
By the end of this lesson you can convert values with CAST and ::, predict the type an expression returns, avoid integer division, and cast text safely by validating it first.
Every value in PostgreSQL has a data type: integer, numeric, text, date, timestamp, boolean and so on. The type decides which operations are allowed and what they return. '2025-11-28' written in quotes is text until something tells PostgreSQL to read it as a date; 7 / 2 is integer arithmetic and returns 3, not 3.5.
Casting converts a value from one type to another. PostgreSQL accepts two spellings that do exactly the same thing.
This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.
Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons