Module 05 · Lesson 3 of 15Detailed study · about 25 min

Read CSV correctly: delimiters, headers and quotes

CSV parsing needs an agreed delimiter, header and quote rule; a filename alone is not enough.

Understand the idea

The .csv extension is a clue, not a complete specification. One producer uses commas, another uses semicolons, and another sends tab-separated fields. Some files include headers; others start immediately with data. Ask for the layout or inspect a small sample.

Key ideas in simple English
ConceptWhat it means
DelimiterSeparates fields outside quotes.
HeaderThe sample has one label row to skip.
QuotesKeep the comma inside Asha’s name within one field.

A concrete example

A spreadsheet export contains the customer name Asha, Rao. Without the correct enclosure setting, the internal comma can shift the apparent column positions. Fixing the parsing contract is more useful than deleting the comma from a real customer’s name.

Remember: A parser follows explicit layout rules. Check the separator, header and enclosure before trusting field positions.

Explain it step by step · full theory

CSV is a set of conventions, not just a filename

The .csv extension is a clue, not a complete specification. One producer uses commas, another uses semicolons, and another sends tab-separated fields. Some files include headers; others start immediately with data. Ask for the layout or inspect a small sample.

For our orders.csv, each line represents one record, commas separate fields and the first line contains labels. A double-quoted name may contain a comma that belongs inside the name.

Follow Asha’s record character by character

The first comma ends order_id 101. The next character opens a quoted field. Inside that field, the comma between Asha and Rao is part of the name. The closing quote ends the enclosure; the following comma separates the name from the amount.

The resulting fields are 101, Asha, Rao as one name, 120.50 and 2025-01-01. There are four fields, not five. Quotation marks describe the CSV structure; they are not part of the parsed customer name.

Give the rules a reusable name

CREATE FILE FORMAT orders_csv_v1 stores the settings in the current schema. TYPE = CSV selects the reader. FIELD_DELIMITER specifies the separator. SKIP_HEADER = 1 skips one header line per file. FIELD_OPTIONALLY_ENCLOSED_BY enables double-quoted fields.

The settings for empty fields and NULL markers are included so that later previews use the same complete format. We will explain those missing-value rules in the next lesson. ESCAPE_UNENCLOSED_FIELD = NONE makes our simple producer contract explicit: unquoted fields do not use a backslash escape convention.

The SQL assumes you have selected an authorized sandbox database and schema. Your role needs USAGE on the database and schema and CREATE FILE FORMAT on the schema. This example creates one object; if that name already exists, inspect it rather than replacing a shared format blindly.

Skipping a header is not mapping by its names

SKIP_HEADER = 1 removes the first line from the data being read. It does not rename $1 to ORDER_ID or automatically match columns to a table. In our previews, $1 means the first field, $2 the second field and so on.

If the producer swaps amount and order_date while leaving the same number of columns, the parser may still read four fields. A successful parse therefore does not prove that each field has the business meaning you expected. Compare sample values and the agreed column order.

Words used in this lesson
Delimiter
A character or sequence separating fields.
Header
An initial line of labels, rather than an order record.
Enclosure
A surrounding character that protects separators within a field.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 4

Read CSV correctly: delimiters, headers and quotes

Five lines
Skip one header
Four data records

Header

Skip the label line, leaving four order records.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Header

    Skip the label line, leaving four order records.

  2. Separator

    A comma outside quotes ends a field.

  3. Enclosure

    The comma inside the quoted name remains part of that one field.

  4. Contract

    Save the agreed interpretation as a named format.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Shared orders.csv and internal-stage setup
Our shared source file

Four orders, one CSV contract

Every CSV lesson uses this fictional orders.csv. One data record represents one order. The first line is a header; the four remaining lines are data. All amounts use the same fictional currency.

order_id,customer_name,amount,order_date
101,"Asha, Rao",120.50,2025-01-01
102,Ben,,2025-01-02
103,Cara,NULL,2025-01-03
104,Dev,0,2025-01-04

Notice the comma inside Asha’s quoted name, Ben’s empty amount, Cara’s literal NULL marker and Dev’s zero. We will follow these exact values from file bytes to parsed fields.

Setup order for optional manual practice
  1. Select an authorized sandbox database and schema. Keep this context consistent across your client sessions.
  2. Create orders_csv_v1 using lesson 3, then orders_stage using lesson 5. Use fresh sandbox names; do not replace existing shared objects.
  3. Save the text above as a UTF-8 file named orders.csv. Upload it with a supported local client or the named-stage upload interface.
  4. Keep only this sample file under the incoming/ prefix when following the four-row expected results.
  5. Select an authorized warehouse for the staged SELECT examples. File previews use compute; this website runs no SQL.

The examples assume the required role privileges described in the lessons. The external-stage and JSON examples have their own additional setup and are clearly marked.

Before running this lesson

Internal-stage learning path. Use L2_OBJECTS_LAB.SALES from Module 03, with an authorized role and warehouse. Create orders_csv_v1 in lesson 3, then orders_stage in lesson 5. Save the exact sample as UTF-8 orders.csv. Keep only this file in incoming/ for the illustrated four-row results. Use fresh names if a setup object already exists.

Create the format used throughout this module

SQL · example
CREATE FILE FORMAT orders_csv_v1
  TYPE = CSV
  FIELD_DELIMITER = ','
  SKIP_HEADER = 1
  FIELD_OPTIONALLY_ENCLOSED_BY = '"'
  EMPTY_FIELD_AS_NULL = TRUE
  NULL_IF = ('NULL')
  ESCAPE_UNENCLOSED_FIELD = NONE;

What to expect and why

Use a selected sandbox database and schema. This defines parsing rules only; it creates no stage, uploads no file and loads no rows. The v1 suffix records the first version of this producer contract.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Why should the comma inside "Asha, Rao" remain in one field?

Try it yourself

A new producer sends semicolon-separated records with no header. Which two settings change?

Compare your reasoning

Use FIELD_DELIMITER = ';' and SKIP_HEADER = 0 in a separate format for that producer. Keep the quote rule only if it matches the new source. Recheck column order and missing-value conventions too.

Official sources and curriculum

Module 05 · See module scope