Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
silviustanimir committed Nov 16, 2023
1 parent f27964b commit da2ae56
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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) to a specified datepart of an input date value, and then returns that modified value. The datepart can be any of the following values for SQL Server and Snowflake: year, quarter, month, week, day, hour, minute, second, millisecond. For Databricks, the datepart can be any of the following values: year, day, hour, minute, second.
This macro adds a specified number value (as a signed integer) to a specified datepart of an input date or datetime, and then returns that modified value. The datepart can be any of the following values for SQL Server and Snowflake: year, quarter, month, week, day, hour, minute, second, millisecond. For Databricks, the datepart can be any of the following values: year, quarter, month, day, hour, minute, second, millisecond.

Usage:
`{{ pm_utils.dateadd('[datepart]', [number], '[expression]') }}`
Expand Down
8 changes: 4 additions & 4 deletions macros/multiple_databases/dateadd.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{%- macro dateadd(datepart, number, datetime_field) -%}
{%- if target.type == 'snowflake' -%}
dateadd({{ datepart }}, {{ number }}, try_to_timestamp(to_varchar({{ datetime_field }})))
dateadd({{ datepart }}, {{ number }}, try_to_timestamp({{ datetime_field }}))
{%- elif target.type == 'sqlserver' -%}
dateadd({{ datepart }}, {{ number }}, try_convert(datetime2, {{ datetime_field }}))
{%- elif target.type == 'databricks' -%}
{%- set clock_component -%}
{%- set time_value -%}
to_unix_timestamp({{ datetime_field }}) - to_unix_timestamp(date_trunc('DD', {{ datetime_field }}))
{%- endset -%}
{%- if datepart == 'second' -%}
Expand All @@ -14,9 +14,9 @@
{%- elif datepart == 'hour' -%}
try_to_timestamp(to_unix_timestamp({{ datetime_field }}) + {{ number }}*3600)
{%- elif datepart == 'day' -%}
try_to_timestamp(to_unix_timestamp(date_add({{ datetime_field }}, {{ number }})) + {{ clock_component }})
try_to_timestamp(to_unix_timestamp(date_add({{ datetime_field }}, {{ number }})) + {{ time_value }})
{%- elif datepart == 'year' -%}
try_to_timestamp(to_unix_timestamp(add_months({{ datetime_field }}, {{ number }}*12)) + {{ clock_component }})
try_to_timestamp(to_unix_timestamp(add_months({{ datetime_field }}, {{ number }}*12)) + {{ time_value }})
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

0 comments on commit da2ae56

Please sign in to comment.