From 0629ae48b8fb6204f973221119cc61a9b000e178 Mon Sep 17 00:00:00 2001 From: johnblust <97988576+johnblust@users.noreply.github.com> Date: Thu, 17 Mar 2022 14:57:21 -0400 Subject: [PATCH 1/4] Update 2021-09-11-sql-dateadd.md --- website/blog/2021-09-11-sql-dateadd.md | 33 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/website/blog/2021-09-11-sql-dateadd.md b/website/blog/2021-09-11-sql-dateadd.md index d9f67bf37cb..280bb9f6f8e 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 macros. + -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 a 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 dbt macros 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. From 53c0c84c013bbe0ce807f433f435b99e9e80f0d1 Mon Sep 17 00:00:00 2001 From: johnblust <97988576+johnblust@users.noreply.github.com> Date: Tue, 22 Mar 2022 15:59:53 -0400 Subject: [PATCH 2/4] Fixed grammer errors --- website/blog/2021-09-11-sql-dateadd.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/blog/2021-09-11-sql-dateadd.md b/website/blog/2021-09-11-sql-dateadd.md index 280bb9f6f8e..5b3a031270a 100644 --- a/website/blog/2021-09-11-sql-dateadd.md +++ b/website/blog/2021-09-11-sql-dateadd.md @@ -18,11 +18,11 @@ I’ve googled the syntax of the dateadd SQL function all of those times except 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 macros. +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. -## What is a DATEADD SQL Function? +## 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. @@ -77,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. -## Standardizing your DATEADD SQL syntax with dbt macros +## Standardizing your DATEADD SQL syntax with dbt macro But couldn’t we be doing something better with those keystrokes, like typing out and then deleting a tweet? From 0206c1fec6c389edf3fd9b26b71c69f1456debcc Mon Sep 17 00:00:00 2001 From: johnblust <97988576+johnblust@users.noreply.github.com> Date: Tue, 22 Mar 2022 16:01:35 -0400 Subject: [PATCH 3/4] Update 2021-09-11-sql-dateadd.md --- website/blog/2021-09-11-sql-dateadd.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/blog/2021-09-11-sql-dateadd.md b/website/blog/2021-09-11-sql-dateadd.md index 5b3a031270a..885ab700021 100644 --- a/website/blog/2021-09-11-sql-dateadd.md +++ b/website/blog/2021-09-11-sql-dateadd.md @@ -77,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. -## Standardizing your DATEADD SQL syntax with dbt macro +## 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? From 1be3506808612abe63bae4ddb17071dedab68091 Mon Sep 17 00:00:00 2001 From: johnblust <97988576+johnblust@users.noreply.github.com> Date: Wed, 30 Mar 2022 10:37:24 -0400 Subject: [PATCH 4/4] Update 2021-09-11-sql-dateadd.md --- website/blog/2021-09-11-sql-dateadd.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/blog/2021-09-11-sql-dateadd.md b/website/blog/2021-09-11-sql-dateadd.md index 885ab700021..0cde1df47cf 100644 --- a/website/blog/2021-09-11-sql-dateadd.md +++ b/website/blog/2021-09-11-sql-dateadd.md @@ -141,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* +