lesson 16 Calculations & data quality · intermediate
CAST, text types & NULL arithmetic
Convert sensor values and calculate deviations with explicit units.
the idea
CAST converts a value to another type. Casting text to INTEGER reads the leading digits and stops at the first non-digit, so '02134-1207' becomes 2134 and leading zeros vanish. Any arithmetic or concatenation with NULL produces NULL; COALESCE supplies a fallback.
Dataset: Logistics
shipments12 columns · 8,200 rows
| shipment_idnumber · unique id | order_referencetext | shipper_nametext | origin_warehouse_idnumber · links to warehouses | destination_citytext | destination_statetext | destination_postal_codetext | service_level_idnumber · links to service_levels | carrier_idnumber · links to carriers | created_atdate & time | promised_delivery_datedate | declared_valuedecimal |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | ORD-643450 | Delta Robotics | 4 | Orlando | FL | 33457 | 3 | 3 | 2025-01-19 21:43:15 | 2025-01-22 | 44.17 |
| 2 | ORD-102115 | Crest Outfitters | 3 | Raleigh | NC | 27481 | 7 | 1 | 2025-04-09 00:16:31 | 2025-04-15 | 114.7 |
| 3 | ORD-926480 | Arbor Systems | 3 | Charlotte | NC | 27558 | 7 | 7 | 2025-03-06 19:19:35 | 2025-03-13 | 50.37 |
| 4 | ORD-651018 | Timber Robotics | 6 | Miami | FL | 33122 | 1 | 12 | 2025-04-05 00:31:02 | 2025-04-11 | 1181.62 |
| 5 | ORD-901762 | APEX ANALYTICS | 4 | San Diego | CA | 97949 | 2 | 3 | 2025-01-20 04:30:44 | 2025-01-29 | 91.96 |
| 6 | ORD-832022 | Nimbus Outfitters | 3 | New York | NY | 10050 | 6 | 5 | 2025-04-03 15:09:38 | 2025-04-11 | 166.67 |
| 7 | ORD-579029 | Atlas Networks | 4 | Las Vegas | NV | 89309 | 1 | 12 | 2025-03-19 00:49:20 | 2025-03-26 | 157.75 |
| 8 | ORD-948815 | Crest Logistics | 5 | Los Angeles | CA | 98905 | 2 | 3 | 2025-01-27 01:46:32 | 2025-02-05 | 80.06 |
| 9 | ORD-654855 | NIMBUS OUTFITTERS | 6 | San Diego | CA | 92755-3782 | 7 | 1 | 2025-04-12 15:36:42 | 2025-04-17 | 37.28 |
| 10 | ORD-345314 | Arbor Energy | 2 | Houston | TX | 77738 | 3 | 10 | 2025-01-23 13:45:39 | 2025-01-27 | 54.76 |
- shipment_idnumber · unique id
- 1
- order_referencetext
- ORD-643450
- shipper_nametext
- Delta Robotics
- origin_warehouse_idnumber · links to warehouses
- 4
- destination_citytext
- Orlando
- destination_statetext
- FL
- destination_postal_codetext
- 33457
- service_level_idnumber · links to service_levels
- 3
- carrier_idnumber · links to carriers
- 3
- created_atdate & time
- 2025-01-19 21:43:15
- promised_delivery_datedate
- 2025-01-22
- declared_valuedecimal
- 44.17
- shipment_idnumber · unique id
- 2
- order_referencetext
- ORD-102115
- shipper_nametext
- Crest Outfitters
- origin_warehouse_idnumber · links to warehouses
- 3
- destination_citytext
- Raleigh
- destination_statetext
- NC
- destination_postal_codetext
- 27481
- service_level_idnumber · links to service_levels
- 7
- carrier_idnumber · links to carriers
- 1
- created_atdate & time
- 2025-04-09 00:16:31
- promised_delivery_datedate
- 2025-04-15
- declared_valuedecimal
- 114.7
- shipment_idnumber · unique id
- 3
- order_referencetext
- ORD-926480
- shipper_nametext
- Arbor Systems
- origin_warehouse_idnumber · links to warehouses
- 3
- destination_citytext
- Charlotte
- destination_statetext
- NC
- destination_postal_codetext
- 27558
- service_level_idnumber · links to service_levels
- 7
- carrier_idnumber · links to carriers
- 7
- created_atdate & time
- 2025-03-06 19:19:35
- promised_delivery_datedate
- 2025-03-13
- declared_valuedecimal
- 50.37
- shipment_idnumber · unique id
- 4
- order_referencetext
- ORD-651018
- shipper_nametext
- Timber Robotics
- origin_warehouse_idnumber · links to warehouses
- 6
- destination_citytext
- Miami
- destination_statetext
- FL
- destination_postal_codetext
- 33122
- service_level_idnumber · links to service_levels
- 1
- carrier_idnumber · links to carriers
- 12
- created_atdate & time
- 2025-04-05 00:31:02
- promised_delivery_datedate
- 2025-04-11
- declared_valuedecimal
- 1181.62
- shipment_idnumber · unique id
- 5
- order_referencetext
- ORD-901762
- shipper_nametext
- APEX ANALYTICS
- origin_warehouse_idnumber · links to warehouses
- 4
- destination_citytext
- San Diego
- destination_statetext
- CA
- destination_postal_codetext
- 97949
- service_level_idnumber · links to service_levels
- 2
- carrier_idnumber · links to carriers
- 3
- created_atdate & time
- 2025-01-20 04:30:44
- promised_delivery_datedate
- 2025-01-29
- declared_valuedecimal
- 91.96
- shipment_idnumber · unique id
- 6
- order_referencetext
- ORD-832022
- shipper_nametext
- Nimbus Outfitters
- origin_warehouse_idnumber · links to warehouses
- 3
- destination_citytext
- New York
- destination_statetext
- NY
- destination_postal_codetext
- 10050
- service_level_idnumber · links to service_levels
- 6
- carrier_idnumber · links to carriers
- 5
- created_atdate & time
- 2025-04-03 15:09:38
- promised_delivery_datedate
- 2025-04-11
- declared_valuedecimal
- 166.67
- shipment_idnumber · unique id
- 7
- order_referencetext
- ORD-579029
- shipper_nametext
- Atlas Networks
- origin_warehouse_idnumber · links to warehouses
- 4
- destination_citytext
- Las Vegas
- destination_statetext
- NV
- destination_postal_codetext
- 89309
- service_level_idnumber · links to service_levels
- 1
- carrier_idnumber · links to carriers
- 12
- created_atdate & time
- 2025-03-19 00:49:20
- promised_delivery_datedate
- 2025-03-26
- declared_valuedecimal
- 157.75
- shipment_idnumber · unique id
- 8
- order_referencetext
- ORD-948815
- shipper_nametext
- Crest Logistics
- origin_warehouse_idnumber · links to warehouses
- 5
- destination_citytext
- Los Angeles
- destination_statetext
- CA
- destination_postal_codetext
- 98905
- service_level_idnumber · links to service_levels
- 2
- carrier_idnumber · links to carriers
- 3
- created_atdate & time
- 2025-01-27 01:46:32
- promised_delivery_datedate
- 2025-02-05
- declared_valuedecimal
- 80.06
- shipment_idnumber · unique id
- 9
- order_referencetext
- ORD-654855
- shipper_nametext
- NIMBUS OUTFITTERS
- origin_warehouse_idnumber · links to warehouses
- 6
- destination_citytext
- San Diego
- destination_statetext
- CA
- destination_postal_codetext
- 92755-3782
- service_level_idnumber · links to service_levels
- 7
- carrier_idnumber · links to carriers
- 1
- created_atdate & time
- 2025-04-12 15:36:42
- promised_delivery_datedate
- 2025-04-17
- declared_valuedecimal
- 37.28
- shipment_idnumber · unique id
- 10
- order_referencetext
- ORD-345314
- shipper_nametext
- Arbor Energy
- origin_warehouse_idnumber · links to warehouses
- 2
- destination_citytext
- Houston
- destination_statetext
- TX
- destination_postal_codetext
- 77738
- service_level_idnumber · links to service_levels
- 3
- carrier_idnumber · links to carriers
- 10
- created_atdate & time
- 2025-01-23 13:45:39
- promised_delivery_datedate
- 2025-01-27
- declared_valuedecimal
- 54.76
first 10 of 8,200 rows · your queries use all of them
worked example
The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.
See messy postal codes as stored. Run this. Some codes lack a leading zero, some include ZIP+4, and some are missing.
step-1.sqlSELECT shipment_id, destination_postal_code FROM shipments WHERE destination_state = 'MA' LIMIT 15;
Try CAST and string concatenation. Run this. CAST changes type; concatenating with NULL yields NULL—COALESCE is what fixes display later.the pattern
example.sqlSELECT CAST('02134' AS INTEGER) AS as_number, CAST(42 AS TEXT) || '!' AS as_text, 'ZIP ' || NULL AS with_null;
common mistake
A ZIP code is an identifier, not a number. CAST(zip AS INTEGER) loses leading zeros and the ZIP+4 suffix.