Snowflake Date and Time Cheat Sheet

How to work with Datetimes in Snowflake SQL

Photo by Eliza Diamond on Unsplash

The following article can be a small cheat sheet for you, if you work a lot with Snowflake or are just learning. The following functions are those, which in my opinion, can be used relatively often.

Function1: To Date

One of the most frequently used functions by me and many others is probably TO_DATEor DATEto convert e.g. a STRING into a date.

SELECT TO_DATE(‘2022–10–17’)

A function that is probably often used by Data Engineers, e.g. when other data types have to be converted from source systems. To prevent errors, you can use the TRY_TO_DATE function which performs the same operation but with error-handling support [1]. The same applies to TO_TIME or TIME, which does not convert a date but a time.

Function 2: Date from Parts

With the function DATE_FROM_PARTSyou can compose a date from single values, which then also has the data type.

Select DATE_FROM_PARTS(2000,2,1)

Function 3: Date Difference

A function that could be interesting for Data Analysts and Data Scientists is the DATEDIFF function. With this you can calculate the difference…

--

--