From de7beb2d38905a520ddf9ae068b6388e87810f36 Mon Sep 17 00:00:00 2001 From: silviu stanimir Date: Tue, 14 Nov 2023 11:29:51 +0100 Subject: [PATCH] feat: implement dateadd macro --- macros/multiple_databases/dateadd.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 macros/multiple_databases/dateadd.sql diff --git a/macros/multiple_databases/dateadd.sql b/macros/multiple_databases/dateadd.sql new file mode 100644 index 0000000..1be2a58 --- /dev/null +++ b/macros/multiple_databases/dateadd.sql @@ -0,0 +1,20 @@ +{%- macro dateadd(datepart, number, date_field) -%} + +{%- if target.type == 'databricks' -%} + {%- if datepart == 'millisecond' -%} + timestamp_millis(unix_millis({{ date_field }}) + {{ number }}) + {%- elif datepart == 'second' -%} + timestamp_seconds(unix_seconds({{ date_field }}) + {{ number }}) + {%- elif datepart == 'minute' -%} + timestamp_seconds(unix_seconds({{ date_field }}) + {{ number }}*60) + {%- elif datepart == 'hour' -%} + timestamp_seconds(unix_seconds({{ date_field }}) + {{ number }}*3600) + {%- elif datepart == 'day' -%} + date_add({{ datepart }}, {{ number }}) + {%- elif datepart == 'week' -%} + date_add({{ datepart }}, {{ number }}*7) + {%- endif -%} +{%- else -%} + dateadd({{ datepart }}, {{ number }}, {{ date_field }}) +{%- endif -%} +{%- endmacro -%}