runtime
33 / 88

lesson 33 Dates, strings & subqueries · intermediate

Clean strings & cast values

Clean text and convert values to the types you need.

the idea

TRIM removes outer spaces, UPPER and LOWER normalize case, REPLACE substitutes text, instr finds a position and substr extracts part of a string. Clean a value in a defined order of steps, and keep NULL as NULL instead of inventing a value.

Dataset: Logistics

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

packages8 columns · 11,505 rows
package_idnumber · unique idshipment_idnumber · links to shipmentstracking_numbertextpackage_typetextweight_kgdecimallength_cmnumberwidth_cmnumberheight_cmnumber
11RT100007919Bbox2.85521530
21RT100015838Cbox1.1694524
32RT10 0023 757Denvelope0.4932242
43RT100031676Ebox2.9351355
54RT100039595Fenvelope0.3832243
65RT100047514Gbox1.9302725
package_idnumber · unique id
1
shipment_idnumber · links to shipments
1
tracking_numbertext
RT100007919B
package_typetext
box
weight_kgdecimal
2.85
length_cmnumber
52
width_cmnumber
15
height_cmnumber
30
package_idnumber · unique id
2
shipment_idnumber · links to shipments
1
tracking_numbertext
RT100015838C
package_typetext
box
weight_kgdecimal
1.1
length_cmnumber
69
width_cmnumber
45
height_cmnumber
24
package_idnumber · unique id
3
shipment_idnumber · links to shipments
2
tracking_numbertext
RT10 0023 757D
package_typetext
envelope
weight_kgdecimal
0.49
length_cmnumber
32
width_cmnumber
24
height_cmnumber
2
package_idnumber · unique id
4
shipment_idnumber · links to shipments
3
tracking_numbertext
RT100031676E
package_typetext
box
weight_kgdecimal
2.93
length_cmnumber
51
width_cmnumber
35
height_cmnumber
5
package_idnumber · unique id
5
shipment_idnumber · links to shipments
4
tracking_numbertext
RT100039595F
package_typetext
envelope
weight_kgdecimal
0.38
length_cmnumber
32
width_cmnumber
24
height_cmnumber
3
package_idnumber · unique id
6
shipment_idnumber · links to shipments
5
tracking_numbertext
RT100047514G
package_typetext
box
weight_kgdecimal
1.9
length_cmnumber
30
width_cmnumber
27
height_cmnumber
25

first 6 of 11,505 rows · your queries use all of them

shipments12 columns · 8,200 rows
shipment_idnumber · unique idorder_referencetextshipper_nametextorigin_warehouse_idnumber · links to warehousesdestination_citytextdestination_statetextdestination_postal_codetextservice_level_idnumber · links to service_levelscarrier_idnumber · links to carrierscreated_atdate & timepromised_delivery_datedatedeclared_valuedecimal
1ORD-643450Delta Robotics4OrlandoFL33457332025-01-19 21:43:152025-01-2244.17
2ORD-102115Crest Outfitters3RaleighNC27481712025-04-09 00:16:312025-04-15114.7
3ORD-926480Arbor Systems3CharlotteNC27558772025-03-06 19:19:352025-03-1350.37
4ORD-651018Timber Robotics6MiamiFL331221122025-04-05 00:31:022025-04-111181.62
5ORD-901762APEX ANALYTICS4San DiegoCA97949232025-01-20 04:30:442025-01-2991.96
6ORD-832022Nimbus Outfitters 3New YorkNY10050652025-04-03 15:09:382025-04-11166.67
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

first 6 of 8,200 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Spot tracking numbers that need cleaning. Run this. Compare raw tracking_number to trimmed, space-stripped, uppercased form.the pattern

    example.sql
    SELECT
      tracking_number,
      UPPER(REPLACE(TRIM(tracking_number), ' ', '')) AS clean
    FROM packages
    WHERE tracking_number <> UPPER(REPLACE(TRIM(tracking_number), ' ', ''))
    LIMIT 10;
  2. Take text before a hyphen. Run this. instr locates '-'; substr keeps the ZIP portion before the suffix.

    step-2.sql
    SELECT
      destination_postal_code,
      instr(destination_postal_code, '-') AS hyphen_at,
      substr(destination_postal_code, 1, instr(destination_postal_code, '-') - 1) AS before_hyphen
    FROM shipments
    WHERE destination_postal_code LIKE '%-%'
    LIMIT 10;

common mistake

Padding with zeros before removing a ZIP+4 suffix pads the wrong text. Split at the hyphen first, then pad.

challenge 1

Return package_id, tracking_number and clean_tracking (trimmed, spaces removed, upper case) for packages whose tracking number is not already in that clean form.

what to return

Columns
package_idtracking_numberclean_tracking in this order
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Clean strings & cast values — Dates, strings & subqueries · Runtime