How to find the difference between two dates in DBT

getDbt

datediff 

The datediff macro can be used to calculate the difference between two dates.

{{ dbt.datediff("column_a", "column_b", "day") }}
{{ dbt.datediff("column", "'2016-03-09'", "month") }}
{{ dbt.datediff("'2016-03-09'", "column", "year") }}

The output based on PostgreSQL can be as follows

((column_b)::date - (column_a)::date)
((date_part('year', ('2023-12-10')::date) - date_part('year', (column)::date))
* 12 + date_part('month', ('2016-03-09')::date) - date_part('month', (column)::date))
(date_part('year', (column)::date) - date_part('year', ('2023-12-10')::date))
Author: user

Leave a Reply