Module 5 · Data that arrives dirty

Stage as text, then cast in a query you can re-run

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 3 of 12 in this chapter

This chapter’s goalLoad an exported CSV with mixed encodings, ambiguous dates and duplicate rows into a typed, deduplicated table by re-runnable query with every dropped row counted, decide and document what each missing value means, and produce a feature table for a model in which no column knows anything that happened after its row’s timestamp.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
Stage as text, then cast in a query you can run againLoad every column astextnothing can fail; theraw table is theevidenceList what will notcastwith a count of eachdistinct valueDecide each oneN/A to NULL, (1,200)to a negativeCast in a viewand next month’s fileneeds no new workA typed load has to be right about every row before any row is in. Loading as text makes the loadtrivially right and moves every decision into SQL, next to a count of the rows that needed it. Whenthat count is not zero, it is a list of decisions still to make, not a reason to add a filter.
A typed load has to be right about every row before any row is in. Loading as text makes the load trivially right and moves every decision into SQL, next to a count of the rows that needed it. When that count is not zero, it is a list of decisions still to make, not a reason to add a filter.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

Let the loader guess and it will guess on the first thousand rows

DuckDB’s CSV reader looks at a sample of the file, decides that a column full of digits is BIGINT, and loads it. The postcode column contains 400001, 110001, 560001 for the first twenty thousand rows and then SW1A 1AA for a customer in London. Depending on the version and the options, that row fails, the whole column falls back to text, or the load stops. Postgres’s COPY does not guess at all; it insists on a table with declared types, and it aborts the entire load on the first value that does not fit, leaving you with nothing.

Both behaviours come from the same fact: a typed load has to be right about every row before any row is in. The way around it is to make the load trivially right by declaring every column as text.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Building Apps with AI exam.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Stage as text, then cast in a query you can re-run’, in Data, SQL and Getting to the Answer. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/data-and-sql/everything-as-text-first (Accessed: 29 November 2026).