Get started with Propel today and receive $15 in monthly credits forever. At any point, upgrade to pay-as-you-go, or contact us to learn more about our volume-based discounts. Visit our pricing page for details.
Discover the power of Snowflake's date functions to manipulate dates with precision and ease
What is the DATEADD function?
Snowflake provides a range of powerful date and time functions that help you manipulate date and time data types in various ways. One such versatile function is the <span class="code-exp">DATEADD</span> function. As the name suggests, it enables you to add or subtract a specific interval to or from a given date or timestamp value.
The <span class="code-exp">DATEADD</span> function has the following syntax,
where
- <span class="code-exp-bracket">interval</span> is the interval you want to add or subtract, such as “DAY”, “HOUR”, “MINUTE”, “SECOND”, etc.
- <span class="code-exp-bracket">value</span> is the number of intervals you want to add to or subtract from the date or timestamp.
- <span class="code-exp-bracket">date_or_timestamp</span> is the date or timestamp value to be adjusted.
How to subtract days from a date
You can subtract days from a date in Snowflake using the <span class="code-exp">DATEADD</span> function. To perform subtraction, simply pass a negative value for the <span class="code-exp-bracket">value</span> parameter.
For example, if you want to subtract 7 days from a date, the syntax would be:
Remember that the <span class="code-exp-bracket">interval</span> parameter must be in uppercase letters. Additionally, when subtracting days, ensure the <span class="code-exp-bracket">value</span> parameter is negative.
Example: TacoSoft Date Manipulation
Let's look at an example using TacoSoft, an imaginary app to manage taquerias. Suppose you have a table called <span class="code-exp">orders</span> with the following columns:
- <span class="code-exp">order_id:</span> Unique identifier for each order.
- <span class="code-exp">order_date:</span> The date when the order was placed.
- <span class="code-exp">customer_id:</span> Unique identifier for each customer.
Your task is to retrieve a list of all orders placed three days before a specific date. In this case, let's say you want to retrieve all orders placed three days before 2023-04-14. You can use the <span class="code-exp">DATEADD</span> function to subtract days from a date in Snowflake and achieve this goal.
Here is the SQL query for this task:
This query would return all the rows in the <span class="code-exp">orders</span> table where the <span class="code-exp">order_date</span> is exactly three days before 2023-04-14.
Conclusion
In this post, we explored Snowflake's <span class="code-exp">DATEADD</span> function to subtract days from a date or timestamp. By using a negative value for the <span class="code-exp-bracket">value</span> parameter, you can easily perform date subtraction. This tool can be valuable for filtering and analyzing time-based data in your Snowflake data warehouse.
Remember that Snowflake offers many other date and time functions to help you manipulate and work with date and time data types. Don't hesitate to explore the official Snowflake documentation to learn more about these functions and how to use them effectively.
Further reading
- Snowflake API: Comprehensive Guide to 3 Methods With Examples
- How to build a Snowflake API
- 5-Minute demo: How to expose your Snowflake data via a blazing-fast GraphQL API
Get started with Propel today and receive $15 in monthly credits forever. At any point, upgrade to pay-as-you-go, or contact us to learn more about our volume-based discounts. Visit our pricing page for details.