diff --git a/website/blog/2021-09-11-sql-dateadd.md b/website/blog/2021-09-11-sql-dateadd.md index d9f67bf37cb..0cde1df47cf 100644 --- a/website/blog/2021-09-11-sql-dateadd.md +++ b/website/blog/2021-09-11-sql-dateadd.md @@ -1,6 +1,6 @@ --- -title: "DATEADD Across Data Warehouses" -description: "Abstract away the need to look up the SQL dateadd function syntax every time you use it, by standardizing your syntax with dbt macros." +title: "DATEADD SQL Function Across Data Warehouses" +description: "DATEADD Function syntax varies across data warehouses. Learn how to standardize your syntax no matter the container." slug: sql-dateadd authors: david_krevitt @@ -16,11 +16,20 @@ I’ve used the dateadd SQL function thousands of times. I’ve googled the syntax of the dateadd SQL function all of those times except one, when I decided to hit the "are you feeling lucky" button and go for it. +In switching between SQL dialects (BigQuery, Postgres and Snowflake are my primaries), I can literally never remember the argument order (or exact function name) of dateadd. + +This article will go over how the DATEADD function works, the nuances of using it across the major cloud warehouses, and how to standardize the syntax variances using dbt macro. + -In switching between SQL dialects (BigQuery, Postgres and Snowflake are my primaries), I can literally never remember the argument order (or exact function name) of dateadd. +## What is the DATEADD SQL Function? + +The DATEADD function in SQL adds a time/date interval to a date and then returns the date. This allows you to add or subtract a certain period of time from a given start date. + +Sounds simple enough, but this function lets you do some pretty useful things like calculating an estimated shipment date based on the ordered date. + -## Differences in dateadd functions across platforms +## Differences in DATEADD syntax across data warehouse platforms All of them accept the same rough parameters, in slightly different syntax and order: @@ -30,7 +39,7 @@ All of them accept the same rough parameters, in slightly different syntax and o The *functions themselves* are named slightly differently, which is common across SQL dialects. -### For example, in Snowflake… +### For example, the DATEADD function in Snowflake… ```sql dateadd( {{ datepart }}, {{ interval }}, {{ from_date }} ) @@ -38,7 +47,13 @@ dateadd( {{ datepart }}, {{ interval }}, {{ from_date }} ) *Hour, minute and second are supported!* -### In BigQuery… +### The DATEADD Function in Databricks + +```sql +date_add( {{ startDate }}, {{ numDays }} ) +``` + +### The DATEADD Function in BigQuery… ```sql date_add( {{ from_date }}, INTERVAL {{ interval }} {{ datepart }} ) @@ -46,7 +61,7 @@ date_add( {{ from_date }}, INTERVAL {{ interval }} {{ datepart }} ) *Dateparts of less than a day (hour / minute / second) are not supported.* -### In Postgres... +### The DATEADD Function in Postgres... Postgres doesn’t provide a dateadd function out of the box, so you’ve got to go it alone - but the syntax looks very similar to BigQuery’s function… @@ -62,7 +77,7 @@ So I made this handy 2 x 2 matrix to help sort the differences out: I am sorry - that’s just a blank 2x2 matrix. I've surrendered to just searching for the docs. -## The tiniest drag on quality of life +## Standardizing your DATEADD SQL syntax with a dbt macro But couldn’t we be doing something better with those keystrokes, like typing out and then deleting a tweet? @@ -84,7 +99,7 @@ Adding 1 month to today would look like... > > *TL;DR: dbt allows data practitioners to write code like software engineers, which in this case means not repeating yourself unnecessarily.* -## Compiling away your dateadd troubles +### Compiling away your DATEADD troubles When we run dbt, the dateadd macro compiles your function into the SQL dialect of the warehouse adapter you’re running on—it’s running the same SQL you would’ve written yourself in your native query browser. @@ -126,3 +141,7 @@ And it’s actually quite a simple 31-line macro ([source here](https://github.c Enjoy! FYI I've used dateadd macro in dbt-utils on BigQuery, Postgres, Redshift and Snowflake, but it likely works across most other warehouses. +*Note: While `dbt_utils` doesn't support Databricks by default, you can use other packages that [implement overrides](https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch#overriding-package-macros) as a workaround.* + +*This [spark_utils package](https://github.com/dbt-labs/spark-utils/blob/main/macros/dbt_utils/cross_db_utils/dateadd.sql) can help you implement the override needed to add support for Databricks dateadd* +