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'
| value | type |
|---|---|
| '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
marketingPrefix tables with marketing.table_name.
idsourcelead_score_labelmarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario