SQL CAST

Explicitly converts a value from one type to another — the standard SQL syntax for it.

What & Why

CAST(value AS type) explicitly converts a value to a different type. Some conversions happen automatically (like comparing a date column to a date-shaped string), but many others need to be requested explicitly with CAST.

See How It Works

BUSINESS QUESTION

A spend stored as text somewhere needs to become a real number before it can be used in arithmetic.

Convert text to a real numberStep 1 of 3
'55000'
valuetype
'55000'text

'55000' starts as text, so arithmetic is not valid yet.

EXAMPLE QUERY
SELECT CAST('55000' AS INTEGER) + 1000;
Now You Try

Practice this concept

Marketing needs lead scores exported as text labels beside each lead id and source.

Available schema
marketing

Prefix tables with marketing.table_name.

idsourcelead_score_label
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario