From 10ff7927245276d5b638ec833d3b6b0b5cd7a133 Mon Sep 17 00:00:00 2001 From: Celine Verhoef Date: Thu, 4 Jan 2024 15:02:29 +0100 Subject: [PATCH] fix: split up the if statement in two approaches The set can't be used in between statements in the same if --- macros/multiple_databases/dateadd.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/macros/multiple_databases/dateadd.sql b/macros/multiple_databases/dateadd.sql index 9b5f318..1a78d2f 100644 --- a/macros/multiple_databases/dateadd.sql +++ b/macros/multiple_databases/dateadd.sql @@ -22,12 +22,13 @@ timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60 * 60 * 24) {%- elif datepart == 'week' -%} timestamp_millis(unix_millis(try_to_timestamp({{ date_or_datetime_field }})) + {{ number_bigint }} * 1000 * 60 * 60 * 24 * 7) + {%- endif -%} {# Since the number of days in a month can differ, use the add_months function for month, quarter and year. Add the time part separately because add_months needs dates as input. Both parts are converted to milliseconds to do the addition. #} {%- set time_in_milliseconds -%} unix_millis(try_to_timestamp({{ date_or_datetime_field }})) - unix_millis(date_trunc('DD', try_to_timestamp({{ date_or_datetime_field }}))) {%- endset -%} - {%- elif datepart == 'month' -%} + {%- if datepart == 'month' -%} timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }}))) + {{ time_in_milliseconds }}) {%- elif datepart == 'quarter' -%} timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ date_or_datetime_field }}), {{ number_bigint }} * 3))) + {{ time_in_milliseconds }})