← Back

#Snowflake

#Snowflake

Chirps tagged with #Snowflake

#Snowflake’s PERIOD data type represents a time range with explicit start and end boundaries, making it easier to query effective dates, detect overlapping ranges, and join historical records based on a specific date. It can also be used to intersect time ranges, such as splitting a work shift across reporting periods, while consistently handling boundaries and reducing complex date comparisons


WITH windows AS (
    SELECT
        PERIOD(TIMESTAMP_NTZ)
          '[2026-09-13 22:00:00, 2026-09-14 06:00:00)' AS duty,
        PERIOD(TIMESTAMP_NTZ)
          '[2026-09-14 00:00:00, 2026-09-21 00:00:00)' AS workweek
), allocation AS (
    SELECT PERIOD_INTERSECT(duty, workweek) AS segment
    FROM windows
)
SELECT DATEDIFF('second', PERIOD_BEGIN(segment), PERIOD_END(segment))
           / 3600.0 AS hours_in_workweek
FROM allocation;
https://medium.com/snowflake/query-time-ranges-with-snowflakes-period-data-type-ce229d2306bd

medium.com

medium.com

IS DISTINCT FROM- NULL-safe comparison in #Snowflake

A Old way: handling NULL specifically


SELECT
FROM users
WHERE country_code != 'US' OR country_code IS NULL

Or wrapping in COALESCE


SELECT
FROM users
WHERE COALESCE (country_code, "N/A') != 'US"

New way!


SELECT
FROM users
WHERE country code IS DISTINCT FROM 'US'