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

fix: datadiff week inconsistency between SQL Server and Snowflake (DNA-17332 / DNA-19251) #66

Merged
merged 1 commit into from
Oct 3, 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
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 -%}