0
0
Snowflakecloud~5 mins

Data types in Snowflake - Commands & Configuration

Choose your learning style9 modes available
Introduction
Data types in Snowflake define the kind of data you can store in each column of a table. Choosing the right data type helps keep your data organized and makes queries faster and more accurate.
When you want to store numbers like prices or counts in your database.
When you need to save text such as names, descriptions, or addresses.
When you want to record dates and times for events or transactions.
When you need to store true or false values for conditions.
When you want to save large objects like images or documents in a table.
Config File - create_table.sql
create_table.sql
CREATE OR REPLACE TABLE example_table (
  id INT,
  name STRING,
  price NUMBER(10,2),
  created_at TIMESTAMP_NTZ,
  is_active BOOLEAN,
  data BINARY
);

This SQL file creates a table named example_table with columns using different Snowflake data types:

  • INT for whole numbers.
  • STRING for text.
  • NUMBER(10,2) for numbers with up to 10 digits and 2 decimals.
  • TIMESTAMP_NTZ for date and time without timezone.
  • BOOLEAN for true/false values.
  • BINARY for storing raw data like files.
Commands
This command creates a new table named example_table with columns of various data types to store different kinds of data.
Terminal
snowsql -q "CREATE OR REPLACE TABLE example_table (id INT, name STRING, price NUMBER(10,2), created_at TIMESTAMP_NTZ, is_active BOOLEAN, data BINARY);"
Expected OutputExpected
Done. Snowflake successfully executed the query.
This command shows the structure of example_table, listing each column and its data type so you can verify the table was created correctly.
Terminal
snowsql -q "DESC TABLE example_table;"
Expected OutputExpected
name | type | kind | null? | default | primary key | unique key | check | expression | comment -----------|----------------|--------|-------|---------|-------------|------------|-------|------------|-------- id | INT | COLUMN | Y | | N | N | | | name | STRING | COLUMN | Y | | N | N | | | price | NUMBER(10,2) | COLUMN | Y | | N | N | | | created_at | TIMESTAMP_NTZ | COLUMN | Y | | N | N | | | is_active | BOOLEAN | COLUMN | Y | | N | N | | | data | BINARY | COLUMN | Y | | N | N | | |
This command inserts a row into example_table with values matching the data types defined for each column.
Terminal
snowsql -q "INSERT INTO example_table (id, name, price, created_at, is_active, data) VALUES (1, 'Coffee', 3.50, CURRENT_TIMESTAMP(), TRUE, TO_BINARY('example'));"
Expected OutputExpected
Done. 1 row inserted.
This command retrieves all rows from example_table to show the stored data and confirm the insert worked.
Terminal
snowsql -q "SELECT * FROM example_table;"
Expected OutputExpected
ID | NAME | PRICE | CREATED_AT | IS_ACTIVE | DATA ---|--------|-------|-----------------------------|-----------|------- 1 | Coffee | 3.50 | 2024-06-01 12:00:00.000000 | true | 6578616D706C65
Key Concept

If you remember nothing else from this pattern, remember: choosing the right data type for each column keeps your data accurate and your queries efficient.

Common Mistakes
Using STRING data type for numbers or dates.
This causes slower queries and can lead to errors when sorting or calculating values.
Use NUMBER for numbers and TIMESTAMP for dates to let Snowflake handle them properly.
Not specifying precision and scale for NUMBER data type.
This can lead to unexpected rounding or storage of more digits than needed.
Always define precision and scale like NUMBER(10,2) for prices with two decimals.
Storing true/false values as strings like 'true' or 'false'.
This wastes space and makes logical operations harder.
Use BOOLEAN data type for true/false values.
Summary
Create tables with columns using appropriate Snowflake data types like INT, STRING, NUMBER, TIMESTAMP, BOOLEAN, and BINARY.
Use SQL commands to create the table, check its structure, insert data, and query the data.
Choosing the right data type helps keep data accurate and queries fast.