Skip to content

Commit

Permalink
fix: align datediff in weeks for SQL Server with Snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis-UiPath committed Oct 3, 2023
1 parent 2df45eb commit 6eb08cd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Usage:
`{{ pm_utils.date_from_timestamp('[expression]') }}`

#### datediff ([source](macros/multiple_databases/datediff.sql))
This macro computes the difference between two date, time, or datetime expressions based on the specified `datepart` and returns an integer value. The datepart can be any of the following values: year, quarter, month, week, day, hour, minute, second, millisecond.
This macro computes the difference between two date, time, or datetime expressions based on the specified `datepart` and returns an integer value. The datepart can be any of the following values: year, quarter, month, week, day, hour, minute, second, millisecond. The difference in weeks is calculated for weeks starting on Monday.

Usage:
`{{ pm_utils.datediff('[datepart]', '[start_date_expression]', '[end_date_expression]') }}`
Expand Down
7 changes: 6 additions & 1 deletion macros/multiple_databases/datediff.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
{%- if target.type == 'snowflake' -%}
datediff({{ datepart }}, {{ start_date_field }}, {{ end_date_field }})
{%- elif target.type == 'sqlserver' -%}
datediff_big({{ datepart }}, {{ start_date_field }}, {{ end_date_field }})
{%- if datepart == 'week' -%}
{# To calculate week differences, weeks start by default on Sunday. Change to align with Snowflake (week starts on Monday) #}
datediff_big({{ datepart }}, dateadd(day, -1, {{ start_date_field }}), dateadd(day, -1, {{ end_date_field }}))
{%- else -%}
datediff_big({{ datepart }}, {{ start_date_field }}, {{ end_date_field }})
{%- endif -%}
{%- endif -%}

{%- endmacro -%}

0 comments on commit 6eb08cd

Please sign in to comment.