Skip to content

Commit

Permalink
fix: split up the if statement in two approaches
Browse files Browse the repository at this point in the history
The set can't be used in between statements in the same if
  • Loading branch information
cverhoef committed Jan 4, 2024
1 parent b757383 commit 8b76679
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion macros/multiple_databases/dateadd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
Expand Down

0 comments on commit 8b76679

Please sign in to comment.