Files
ISSSTELEON/Transformation/macros/datediff.sql
T

23 lines
1.0 KiB
SQL

{#
dbt's `datediff` on SQLite, which has no `datediff` function: whole days between two dates, through
`julianday`. Only days are needed so far; any other part fails at compile time instead of returning a
wrong number. The other engines keep their own `datediff`.
#}
{% macro sqlite__datediff(first_date, second_date, datepart) -%}
{%- if datepart | lower != 'day' -%}
{{ exceptions.raise_compiler_error("datediff on SQLite only supports 'day', not '" ~ datepart ~ "'") }}
{%- endif -%}
cast(julianday({{ second_date }}) - julianday({{ first_date }}) as integer)
{%- endmacro %}
{#
On MySQL (dbt-mysql 1.7 has no `datediff` of its own, so dbt would write SQL Server's form): MySQL's
`DATEDIFF(a, b)` is the whole days from b to a.
#}
{% macro mysql__datediff(first_date, second_date, datepart) -%}
{%- if datepart | lower != 'day' -%}
{{ exceptions.raise_compiler_error("datediff on MySQL only supports 'day', not '" ~ datepart ~ "'") }}
{%- endif -%}
datediff({{ second_date }}, {{ first_date }})
{%- endmacro %}