forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
last_day.sql
34 lines (25 loc) · 926 Bytes
/
last_day.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
This function has been tested with dateparts of month and quarters. Further
testing is required to validate that it will work on other dateparts.
*/
{% macro last_day(date, datepart) %}
{{ adapter_macro('dbt_utils.last_day', date, datepart) }}
{% endmacro %}
{%- macro default_last_day(date, datepart) -%}
cast(
{{dbt_utils.dateadd('day', '-1',
dbt_utils.dateadd(datepart, '1', dbt_utils.date_trunc(datepart, date))
)}}
as date)
{%- endmacro -%}
{% macro default__last_day(date, datepart) -%}
{{dbt_utils.default_last_day(date, datepart)}}
{%- endmacro %}
{% macro postgres__last_day(date, datepart) -%}
{%- if datepart == 'quarter' -%}
{{ exceptions.raise_compiler_error(
"dbt_utils.last_day is not supported for datepart 'quarter' on this adapter") }}
{%- else -%}
{{dbt_utils.default_last_day(date, datepart)}}
{%- endif -%}
{%- endmacro %}