Manipulating datetime data

Data Types and Functions in Snowflake

Jake Roach

Field Data Engineer

Extracting timestamp components

Snowflake provides functions to extract components from a timestamp

SELECT
    <fields>,

    DAY(<1>)  -- Many other options!

FROM ...;

<1>: DATE, TIME, or TIMESTAMP

  • DAY
  • MONTH
  • YEAR
  • HOUR
  • MINUTE
  • SECOND
  • DAYNAME
  • MONTHNAME
Data Types and Functions in Snowflake

Parsing timestamps

SELECT
    exam_completed_time,

    YEAR(exam_completed_time) AS y,
    MONTHNAME(exam_completed_time) AS mn,
    DAYNAME(exam_completed_time) AS dn,
    DAY(exam_completed_time) AS d,

    HOUR(exam_completed_time) AS h,
    MINUTE(exam_completed_time) AS m,
    SECOND(exam_completed_time) AS s

FROM STUDENTS.grades;
Data Types and Functions in Snowflake

Parsing timestamps

     exam_completed_time  |    y   |   mn  |   dn  |   d  |   h  |   m  |   s 
    --------------------- | ------ | ----- | ----- | ---- | ---- | ---- | ----

     2025-06-25 11:17:02  |  2025  |  Jun  |  Wed  |  25  |  11  |   6  |   2

     2025-06-26 08:49:49  |  2025  |  Jun  |  Thu  |  26  |   8  |  49  |  49
     2025-06-25 09:56:07  |  2025  |  Jun  |  Wed  |  25  |   9  |  56  |   7
     2025-06-24 22:14:27  |  2025  |  Jun  |  Tue  |  24  |  22  |  14  |  27
     2025-06-26 13:36:55  |  2025  |  Jun  |  Thu  |  26  |  13  |  36  |  55
     2025-06-25 16:23:09  |  2025  |  Jun  |  Wed  |  25  |  16  |  23  |   9

                                        ...
Data Types and Functions in Snowflake

DATEDIFF

DATEDIFF finds the interval between two dates or timestamps

SELECT
    <fields>,


DATEDIFF(<1>, <2>, <3>) -- unit, first timestamp, second timestamp
FROM ...;

<1>: Unit of time the result will be in, such as MINUTE, HOUR, DAY, YEAR, WEEK, etc.

<2>:Starting timestamp

<3>: Ending timestamp

Data Types and Functions in Snowflake

Using DATEDIFF

First TIMESTAMP:  2025-05-12 08:24:08
Second TIMESTAMP: 2025-11-13 03:05:46
SELECT 
    DATEDIFF(
        DAY, 
        TO_DATE('2025-05-12 08:24:08'), 
        TO_DATE('2025-11-13 03:05:46')
    )
                                   185 (DAYS)
Data Types and Functions in Snowflake

DATEADD

SELECT

    -- unit, number of units, timestamp
    DATEADD(
        DAY, 
        185, 
        TO_DATE('2025-05-12 08:24:08')
    )

FROM ...;
2025-05-12 08:24:08 + 185 DAYs

         = 2025-11-13 08:24:08

Add intervals of time to DATE, TIME, or TIMESTAMP using DATEADD

$$

<1>: Unit of time to add, such as MINUTE, HOUR, DAY, YEAR, WEEK, etc.

<2>: # of "units" to add to timestamp

<3>: The timestamp that will be added to

Data Types and Functions in Snowflake

Manipulate a DATE

SELECT
    s_id,
    exam_completed_time AS completed_time,
    exam_due_time AS due_time,

    -- Find the difference in time between completion and due date
    DATEDIFF(HOUR, exam_completed_time, exam_due_time) AS hours_early,

    -- Determine date that teacher must complete grading by
    DATEADD(WEEK, 1, exam_completed_time) AS grading_due

FROM STUDENTS.grades;
Data Types and Functions in Snowflake

Manipulate a DATE


  s_id  |     completed_time    |        due_time       |  hours_early  |      grading_due
 ------ | --------------------- | --------------------- | ------------- | ---------------------

   919  |  2025-06-25 11:17:02  |  2025-06-26 10:00:00  |       22      |  2025-07-02 11:17:02

   871  |  2025-06-26 08:49:49  |  2025-06-26 10:00:00  |        1      |  2025-07-03 08:49:49
   111  |  2025-06-25 09:56:07  |  2025-06-26 10:00:00  |       24      |  2025-07-02 09:56:07
   465  |  2025-06-24 22:14:27  |  2025-06-26 10:00:00  |       35      |  2025-07-01 22:14:27
   248  |  2025-06-26 13:36:55  |  2025-06-26 10:00:00  |       -3      |  2025-07-23 13:36:55
   767  |  2025-06-25 16:23:09  |  2025-06-26 10:00:00  |       17      |  2025-07-02 16:23:09

                                           ...

Data Types and Functions in Snowflake

Let's practice!

Data Types and Functions in Snowflake

Preparing Video For Download...