runtime
80 / 88

lesson 80 Warehouse & reporting projects · advanced

Validate calendar dates before converting

Validate real calendar dates before converting text.

the idea

Eight digits do not prove a valid date. Shape YYYYMMDD into YYYY-MM-DD, require exactly eight digits, and keep the candidate only if date(candidate, '+0 days') returns the same text; SQLite normalizes impossible dates such as 2011-11-31 to another day, which exposes them. Invalid values become NULL while the source row is kept.

Dataset: Retail

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.

raw_dates2 columns · 584 rows
row_idnumber · unique idraw_datetext
613920111101
614020111101
614120111101
614220111101
614320111101
614420111101
row_idnumber · unique id
6139
raw_datetext
20111101
row_idnumber · unique id
6140
raw_datetext
20111101
row_idnumber · unique id
6141
raw_datetext
20111101
row_idnumber · unique id
6142
raw_datetext
20111101
row_idnumber · unique id
6143
raw_datetext
20111101
row_idnumber · unique id
6144
raw_datetext
20111101

first 6 of 584 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. See invalid dates normalize away. Run this. SQLite rolls impossible dates like 2011-11-31 forward—comparing normalized output to your candidate is how you reject bad calendar values.the pattern

    example.sql
    SELECT
      date('2011-11-31', '+0 days') AS normalized,
      date('2011-02-29', '+0 days') AS not_a_leap_year;
  2. Group raw date string lengths. Most export values are eight characters; shorter or longer strings fail the format gate before you parse them.

    step-2.sql
    SELECT raw_date, length(raw_date) AS chars, COUNT(*) AS rows
    FROM raw_dates
    GROUP BY length(raw_date)
    ORDER BY rows DESC;
  3. Test eight-digit shape with GLOB. GLOB confirms YYYYMMDD shape before you build candidate and run the calendar check.

    step-3.sql
    SELECT
      raw_date,
      raw_date GLOB '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' AS eight_digits
    FROM raw_dates
    LIMIT 10;

common mistake

date('2011-11-31') returns '2011-12-01' rather than NULL; comparing the result with the candidate is what rejects it.

challenge 1

Return row_id, raw_date and clean_date for every raw_dates row: the YYYY-MM-DD date when raw_date is exactly eight digits forming a real calendar date, otherwise NULL.

what to return

Columns
row_idraw_dateclean_date 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.
Validate calendar dates before converting — Warehouse & reporting projects · Runtime