Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dateadd macro DNA-15944 [DNA-20679] #80

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This dbt package contains macros for SQL functions to run the dbt project on mul
- [as_varchar](#as_varchar-source)
- [charindex](#charindex-source)
- [create_index](#create_index-source)
- [date_add](#date_add-source)
- [date_from_timestamp](#date_from_timestamp-source)
- [datediff](#datediff-source)
- [string_agg](#string_agg-source)
Expand Down Expand Up @@ -86,6 +87,12 @@ In case you want to create the index on a source table, refer to the table using
) }}
```

#### dateadd ([source](macros/multiple_databases/dateadd.sql))
This macro adds a specified number value (as a signed integer) of the specified datepart to an input date or datetime and then returns that modified value. The datepart can be any of the following values: year, quarter, month, week, day, hour, minute, second, millisecond.

Usage:
`{{ pm_utils.dateadd('[datepart]', [number], '[expression]') }}`

#### date_from_timestamp ([source](macros/multiple_databases/date_from_timestamp.sql))
This macro extracts the date part from a datetime field.

Expand Down
33 changes: 33 additions & 0 deletions macros/multiple_databases/dateadd.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{%- macro dateadd(datepart, number, date_or_datetime_field) -%}
{%- if target.type == 'snowflake' -%}
dateadd({{ datepart }}, {{ number }}, try_to_timestamp(to_varchar({{ date_or_datetime_field }})))
{%- elif target.type == 'sqlserver' -%}
dateadd({{ datepart }}, {{ number }}, try_convert(datetime2, {{ date_or_datetime_field }}))
{%- elif target.type == 'databricks' -%}
{%- set datetime_field -%}
try_to_timestamp({{ date_or_datetime_field }})
{%- endset -%}
{%- set time_field -%}
unix_millis({{ datetime_field }}) - unix_millis(date_trunc('DD', {{ datetime_field }}))
{%- endset -%}
{%- if datepart == 'millisecond' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }})
{%- elif datepart == 'second' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*1000)
{%- elif datepart == 'minute' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*60000)
{%- elif datepart == 'hour' -%}
timestamp_millis(unix_millis({{ datetime_field }}) + {{ number }}*3600000)
{%- elif datepart == 'day' -%}
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number }}))) + {{ time_field }})
{%- elif datepart == 'week' -%}
timestamp_millis(unix_millis(try_to_timestamp(date_add(to_date({{ datetime_field }}), {{ number }}*7))) + {{ time_field }})
{%- elif datepart == 'month' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}))) + {{ time_field }})
{%- elif datepart == 'quarter' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}*3))) + {{ time_field }})
{%- elif datepart == 'year' -%}
timestamp_millis(unix_millis(try_to_timestamp(add_months(to_date({{ datetime_field }}), {{ number }}*12))) + {{ time_field }})
{%- endif -%}
{%- endif -%}
{%- endmacro -%}