forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlast_day.sql
50 lines (37 loc) · 1.48 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
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_sqlserver.last_day', date, datepart) }}
{% endmacro %}
{%- macro default_last_day(date, datepart) -%}
cast(
{{dbt_utils_sqlserver.dateadd('day', '-1',
dbt_utils_sqlserver.dateadd(datepart, '1', dbt_utils_sqlserver.date_trunc(datepart, date))
)}}
as date)
{%- endmacro -%}
{% macro default__last_day(date, datepart) -%}
{{dbt_utils_sqlserver.default_last_day(date, datepart)}}
{%- endmacro %}
{% macro postgres__last_day(date, datepart) -%}
{%- if datepart == 'quarter' -%}
{{ exceptions.raise_compiler_error(
"dbt_utils_sqlserver.last_day is not supported for datepart 'quarter' on this adapter") }}
{%- else -%}
{{dbt_utils_sqlserver.default_last_day(date, datepart)}}
{%- endif -%}
{%- endmacro %}
{% macro sqlserver__last_day(date, datepart) -%}
{%- if datepart == 'quarter' -%}
{{ exceptions.raise_compiler_error(
"dbt_utils_sqlserver.last_day is not supported for datepart 'quarter' on this adapter") }}
{%- elif datepart == 'month' -%}
EOMONTH ( {{ date }})
{%- elif datepart == 'year' -%}
cast(cast(year({{ date }}) as varchar(4)) + '-12-31' as date)
{%- else -%}
{{dbt_utils_sqlserver.default_last_day(date, datepart)}}
{%- endif -%}
{%- endmacro %}