From da2ae56c0899c5c56166be8a7098b8d9ea6cb38a Mon Sep 17 00:00:00 2001 From: silviu stanimir Date: Thu, 16 Nov 2023 09:17:13 +0100 Subject: [PATCH] fix --- README.md | 2 +- macros/multiple_databases/dateadd.sql | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3efd028..1514c95 100644 --- a/README.md +++ b/README.md @@ -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]') }}` diff --git a/macros/multiple_databases/dateadd.sql b/macros/multiple_databases/dateadd.sql index 971509d..7bc7578 100644 --- a/macros/multiple_databases/dateadd.sql +++ b/macros/multiple_databases/dateadd.sql @@ -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' -%} @@ -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 -%}