Snowflake SQL data types

Introduction to Snowflake SQL

George Boorman

Senior Curriculum Manager, DataCamp

Common data types

Category Data types
Text/string VARCHAR, CHAR, TEXT
Introduction to Snowflake SQL

Common data types

Category Data types
Text/string VARCHAR, CHAR, TEXT
Numeric INTEGER
Introduction to Snowflake SQL

Common data types

Category Data types
Text/string VARCHAR, CHAR, TEXT
Numeric INTEGER
Boolean BOOLEAN
Introduction to Snowflake SQL

Common data types

Category Data types
Text/string VARCHAR, CHAR, TEXT
Numeric INTEGER
Boolean BOOLEAN
Date/time DATE, TIME, TIMESTAMP
1 https://docs.snowflake.com/en/sql-reference/intro-summary-data-types
Introduction to Snowflake SQL

Snowflake SQL data types - NUMBER

NUMBER(p, s)

 

  • NUMERIC works in Snowflake as an alias for NUMBER
  • p = precision; s = scale
  • Max p and s values: 38
    • Exceeding will cause rounding!
Introduction to Snowflake SQL

Snowflake SQL data types - TIMESTAMP_LTZ

  • TIMESTAMP_LTZ
    • Combines DATE and TIME with local time zone
    • Format: YYYY-MM-DD HH:MI:SS

 

CREATE TABLE orders (
  -- Timestamp with local time zone
  order_timestamp TIMESTAMP_LTZ
  )

Timestamp local time zone data

Introduction to Snowflake SQL

Data type conversion - What?

  • Converting data from one type to another

Order quantity column data from text to number

Introduction to Snowflake SQL

Data type conversion - Why?

  • Improving performance
  • Data accuracy and consistency
  • Data quality
Introduction to Snowflake SQL

Data type conversion - How?

  1. CAST

    Syntax:

    • CAST( <source_data/column> AS <target_data_type> )

    • CAST('80' AS INT)

  2. ::

    Syntax:

    • <source_data/column>::<target_data_type>

    • '80'::INT

Introduction to Snowflake SQL

CAST

SELECT CAST(order_timestamp AS DATE) 
       AS order_date 
FROM orders
Introduction to Snowflake SQL

CAST results

Before casting order_timestamp_ltz

After casting order date data

Introduction to Snowflake SQL

Conversion functions

  • Examples: TO_VARCHAR, TO_DATE, etc.

TO_VARCHAR

  • TO_VARCHAR( <expr> )

    • expr - numeric, timestamp, etc.

    • Result: VARCHAR

Example:

SELECT TO_VARCHAR(86)

Result:

86

Introduction to Snowflake SQL

Checking data types

DESC TABLE orders
name        type            kind     null?    default     primary key    
ORDER_ID    NUMBER(38,0)    COLUMN   N        null        Y    
ORDER_DATE  DATE            COLUMN   Y        null        N    
ORDER_TIME  TIME(9)         COLUMN   Y        null        N
Introduction to Snowflake SQL

Let's practice!

Introduction to Snowflake SQL

Preparing Video For Download...