Commonly used Snowflake Date Functions and Examples

  • Post author:
  • Post last modified:November 18, 2022
  • Post category:Snowflake
  • Reading time:11 mins read

The date functions are most commonly used functions in the data warehouse. You can use date functions to manipulate the date expressions or variables containing date and time value. For example, get the current date, subtract date values, etc. In this article, we will check what are commonly used date functions in the Snowflake cloud data warehouse.

Commonly used Snowflake Date Functions and Examples

Many applications use date functions to manipulate the date and time data types. Each date value contains the century, year, month, day, hour, minute, second and milliseconds. Hence, date types are highly formatted and complicated data types. Almost all relational databases supports the date functions. The date functions in Snowflake are same or slightly different compared to other RDBMS.

Snowflake Date Functions

The date Functions are sub-divided into 7 types of functions.

  • Current Date/Timestamp Functions
  • Date/Timestamp Construction Functions
  • Date/Time Part Extraction Functions
  • Addition/Subtraction Date Functions
  • Date/Time Truncation Functions
  • Date Conversion Functions
  • Timezone Conversion Functions

Now, let us check these functions in detail.

Current Date/Timestamp Functions in Snowflake

Following are the current date and timestamp functions.

Function Description
CURRENT_DATECurrent system date
CURRENT_TIMECurrent system time
CURRENT_TIMESTAMPCurrent system date with timestamp

Date/Timestamp Construction Functions in Snowflake

Following are the date and time construction functions.

Function Description
DATE_FROM_PARTSCreates a date from individual numeric components: year, month, day
TIME_FROM_PARTS Creates a time from individual numeric components: hour, minutes, seconds, nanoseconds
TIMESTAMP_FROM_PARTS Creates a timestamp from individual numeric components

Date/Time Part Extraction Functions in Snowflake

Following are the date and time part extraction functions.

Function Description
DATE_PARTExtract specified date and time part.
EXTRACTExtract specified date and time part.
DAYNAME Extract 3 letter day of the week.
HOURExtract hour
MINUTEExtract minutes
SECOND Extract Seconds
LAST_DAYReturns the last day of the specified date part for a date or timestamp.
MONTHNAMEExtract 3 letter month name from date/timestamp.
NEXT_DAYReturns day of the week that comes after the input date.
PREVIOUS_DAY Returns day of the week that comes before the input date.
YEAR/ YEAROFWEEK Returns year
YEAROFWEEKISO Same as YEAROFWEEK, except uses ISO semantics .
DAY/ DAYOFMONTH Day of the month.
DAYOFWEEK Day of the week.
DAYOFWEEKISOThe Day of the week uses ISO semantics .
DAYOFYEARDay of the year
WEEK/ WEEKOFYEAR Week of the year
WEEKISO Week of the year uses ISO semantics.
MONTHReturns month
QUARTERReturns quarter

Related Articles,

Date Addition/Subtraction Functions in Snowflake

Following are the date addition/subtraction functions.

Function Description
ADD_MONTHSAdds or subtracts a specified number of months to a date or timestamp.
DATEADDAdds the specified value for the specified date or time part to a date, time, or timestamp.
DATEDIFFCalculate difference between two dates and return date part.
TIMEADDAdds the specified value for the specified date or time part to a date, time, or timestamp.
TIMEDIFFCalculate difference between two time expressions and return date part.
TIMESTAMPADDAdds the specified value for the specified date or time part to a date, time, or timestamp.
TIMESTAMPDIFFCalculate difference between two timestamp expressions and return date part.

Date/Time Truncation Functions in Snowflake

Following are the date/time truncate functions.

FunctionDescription
DATE_TRUNCTruncates a DATE, TIME, or TIMESTAMP to the specified precision.
TIME_SLICECalculates the beginning or end of a “slice” of time.
TRUNCTruncates a date, time, or timestamp to the specified part.

Date Conversion Functions in Snowflake

Following are date conversion functions.

FunctionDescription
TO_DATEConvert string containing date to date type.
TO_TIMEConvert string containing time to time type.
TO_TIMESTAMPConvert string containing timestamp to timestamp type.
CASTConvert string containing date to date or date time format.
::The cast operator is alternative to CAST function.

Related Article,

Timezone Conversion Functions in Snowflake

Following function can be used to convert timezone.

FunctionDescription
CONVERT_TIMEZONEConverts a timestamp to another time zone.

Snowflake Date Function Examples

Following are some of the date function usage examples.

  • Get current date, time and timestamp value in Snowflake

For example,

select CURRENT_DATE;
+--------------+
| CURRENT_DATE |
|--------------|
| 2020-01-11   |
+--------------+

select CURRENT_TIMESTAMP;
+-------------------------------+
| CURRENT_TIMESTAMP             |
|-------------------------------|
| 2020-01-11 10:22:06.433 -0800 |
+-------------------------------+

select CURRENT_TIME;
+--------------+
| CURRENT_TIME |
|--------------|
| 10:22:25     |
+--------------+
  • Subtract month to current date and convert it to YYYYMMDD format in Snowflake

For example,

select to_char(add_months(current_date, -1), 'YYYYMMDD') as int;
+----------+
| YYYYMMDD |
|----------|
| 20191211 |
+----------+
  • Add month to current date and convert it to integer format in Snowflake

For example,

select to_number(to_char(add_months(current_date, 1),'YYYYMMDD'),'99999999') as INT;
+----------+
|      INT |
|----------|
| 20200211 |
+----------+
  • Extract year,day,month part from the current date in Snowflake
select extract (year from current_date) as yr;
+------+
|   YR |
|------|
| 2020 |
+------+

select date_trunc('year', current_date) as yr;
+------------+
| YR         |
|------------|
| 2020-01-01 |
+------------+

select extract (day from current_date) as day;
+-----+
| DAY |
|-----|
|  11 |
+-----+

select extract (month from current_date) as mon;
+-----+
| MON |
|-----|
|   1 |
+-----+
  • Construct Date from Individual Numeric Parts

For example,

select DATE_FROM_PARTS(2020, 1, 20) as dt;
+------------+
| DT         |
|------------|
| 2020-01-20 |
+------------+
  • Get first and last day of previous month in Snowflake

For examples,

select last_day(current_date - interval '2 month') + interval '1 day' as first_day;
+------------+
| FIRST_DAY  |
|------------|
| 2019-12-01 |
+------------+

select last_day( current_date - interval '1 month' ) as last_day;
+------------+
| LAST_DAY   |
|------------|
| 2019-12-31 |
+------------+

Related Articles,

Hope this helps 🙂