Snowflake's built-in functions are organized into broad categories such as aggregate, window, string, date and time, semi-structured data, conditional, mathematical, and conversion functions. This guide lists them by usage so you can quickly find the right function for a given task.
Why Snowflake Functions Are Grouped by Usage
Snowflake provides hundreds of built-in functions, and they are documented in categories that reflect common analytical tasks. Grouping by usage helps you choose the right function without memorizing every name. The main categories are aggregate, window, string, date and time, semi-structured data, conditional, mathematical, conversion, and system functions. Each category serves a distinct purpose in data processing and analysis.
Aggregate Functions
Aggregate functions summarize multiple rows into a single value. They are commonly used with GROUP BY clauses to produce summary statistics.
- COUNT: returns the number of rows or non-null values.
- SUM: adds numeric values.
- AVG: calculates the average of numeric values.
- MIN and MAX: return the smallest and largest values.
- STDDEV and VARIANCE: measure statistical dispersion.
- ARRAY_AGG: collects values into an array.
- OBJECT_AGG: builds an object from key-value pairs.
These functions ignore NULLs unless otherwise specified. For example, COUNT(column) counts non-null values, while COUNT(*) counts all rows.
Window Functions
Window functions perform calculations across a set of rows related to the current row, without collapsing the result set. They are essential for ranking, running totals, and moving averages.
- ROW_NUMBER: assigns a unique sequential integer to each row within a partition.
- RANK and DENSE_RANK: rank rows with gaps or without gaps.
- NTILE: divides rows into a specified number of buckets.
- LAG and LEAD: access data from previous or subsequent rows.
- SUM, AVG, MIN, MAX as window functions: compute cumulative or partitioned aggregates.
Window functions require an OVER clause that defines the partitioning and ordering. They are powerful for time-series analysis and cohort comparisons.
String Functions
String functions manipulate text data. They are frequently used for cleaning, parsing, and formatting.
- CONCAT: joins two or more strings.
- SUBSTR or SUBSTRING: extracts a portion of a string.
- LENGTH: returns the number of characters.
- UPPER and LOWER: change case.
- TRIM, LTRIM, RTRIM: remove whitespace or specified characters.
- REPLACE: substitutes occurrences of a substring.
- SPLIT: divides a string into an array based on a delimiter.
- REGEXP_SUBSTR and REGEXP_REPLACE: perform regular expression operations.
These functions are useful for standardizing addresses, extracting domains from emails, and similar tasks.
Date and Time Functions
Date and time functions handle temporal data, including extraction, arithmetic, and formatting.
- CURRENT_DATE, CURRENT_TIMESTAMP: return the current date or timestamp.
- DATEADD: adds an interval to a date or timestamp.
- DATEDIFF: calculates the difference between two dates.
- DATE_TRUNC: truncates a date to a specified precision (e.g., month, year).
- EXTRACT or DATE_PART: retrieves a component such as year, month, or day.
- TO_DATE, TO_TIMESTAMP: convert strings to date or timestamp values.
These functions are critical for time-based reporting and cohort analysis.
Semi-Structured Data Functions
Snowflake excels at handling semi-structured data like JSON, Avro, and Parquet. These functions extract and manipulate such data.
- PARSE_JSON: converts a string to a JSON object.
- GET_PATH or : operator: extracts a value from a JSON object using a path.
- FLATTEN: converts nested arrays or objects into rows.
- ARRAY_CONSTRUCT, OBJECT_CONSTRUCT: build arrays and objects.
- ARRAY_SIZE: returns the number of elements in an array.
These functions enable querying nested data without transforming it first.
Conditional and Conversion Functions
Conditional functions handle logic, while conversion functions change data types.
- CASE: evaluates conditions and returns a result.
- IFF: a shorthand for CASE with two outcomes.
- COALESCE: returns the first non-null value.
- NULLIF: returns null if two expressions are equal.
- CAST or TO_
: converts a value to a specified data type. - TRY_CAST: returns null instead of an error on failed conversion.
These functions are essential for data cleaning and transformation pipelines.
Mathematical and System Functions
Mathematical functions perform numeric operations, and system functions provide metadata.
- ABS, ROUND, CEIL, FLOOR: basic numeric operations.
- POWER, SQRT: exponentiation and square root.
- RANDOM: generates a random number.
- CURRENT_USER, CURRENT_ROLE: return session context.
- SYSTEM$TYPEOF: returns the data type of an expression.
These functions are useful for calculations and debugging.
Frequently Asked Questions
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts all rows in a table, including those with nulls. COUNT(column) counts only non-null values in that column.
Can I use window functions without a GROUP BY?
Yes, window functions operate on the result set after any grouping and do not require a GROUP BY clause. They use an OVER clause to define the window.
How do I extract a value from a JSON column?
Use the colon operator or GET_PATH. For example, json_column:key returns the value associated with "key".
What is the difference between CAST and TRY_CAST?
CAST raises an error if conversion fails, while TRY_CAST returns null on failure, making it safer for messy data.
Are Snowflake functions case-sensitive?
Function names are case-insensitive. However, string comparisons and identifiers may be case-sensitive depending on context.
Can I create my own functions?
Yes, Snowflake supports user-defined functions (UDFs) in SQL, JavaScript, and other languages. They can be used alongside built-in functions.