---
title: "ClickHouse® tips #5: Adding and subtracting intervals"
excerpt: "Tips and recipes to learn how to make the most of ClickHouse®, curated weekly by the Tinybird team. Part 5."
authors: "Alberto Romeu"
categories: "The Data Base"
createdOn: "2023-09-25 00:00:00"
publishedOn: "2021-03-24 00:00:00"
updatedOn: "2022-09-08 00:00:00"
status: "published"
---

<p>ClickHouse® lets you add and subtract time and date intervals. For example, to subtract 1 year to the current date you would to</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20now%28%29%20-%20toIntervalYear%281%29%0A&amp;language=Tinybird&amp;title=subtract_1_year&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>You can also add or subtract more than one interval at a time:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20%28%28now%28%29%20-%20toIntervalYear%281%29%29%20%2B%20toIntervalMonth%281%29%29%20-%20toIntervalHour%281%29%0A&amp;language=Tinybird&amp;title=subtract_several_interevals&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>And the <code>INTERVAL</code> syntax you’ve used in other databases like Postgres is also <a href="https://clickhouse.tech/docs/en/sql-reference/data-types/special-data-types/interval/">supported</a>. One advantage of it is that it lets you get rid of parentheses. So the previous query can be rewritten as follows:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20now%28%29%20-%20interval%201%20year%20%2B%20interval%201%20month%20-%20interval%201%20hour%0A&amp;language=Tinybird&amp;title=using_interval&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>These are all the intervals supported, from the <a href="https://clickhouse.tech/docs/en/sql-reference/data-types/special-data-types/interval/">docs</a>:</p><ul><li><code>SECOND</code></li><li><code>MINUTE</code></li><li><code>HOUR</code></li><li><code>DAY</code></li><li><code>WEEK</code></li><li><code>MONTH</code></li><li><code>QUARTER</code></li><li><code>YEAR</code></li></ul><p>So you could get it as complex as you want:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20%0A%20%20%20%20toStartOfYear%28now%28%29%29%0A%20%20%20%20-%20interval%201%20year%0A%20%20%20%20%2B%20interval%202%20quarter%0A%20%20%20%20-%20interval%201%20month%0A%20%20%20%20-%20interval%201%20hour%0A%20%20%20%20%2B%20interval%2023%20minute%0A%20%20%20%20-%20interval%203%20second%0A&amp;language=Tinybird&amp;title=using_interval_complex&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure><p>The smallest interval precision supported is second, and if you want to go lower than that there are some workarounds like the described in <a href="https://github.com/ClickHouse®/ClickHouse®/issues/6216">this issue</a>.</p><p>Finally, note that <code>now()</code>, interval and all the interval types (<code>day</code>, <code>year</code>…) are case-insensitive, so you can use their uppercase versions as well:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/endpoint?code=SELECT%20NOW%28%29%20%2B%20INTERVAL%201%20YEAR%2C%20now%28%29%20%2B%20interval%201%20year%0A&amp;language=Tinybird&amp;title=case_insensitive_functions&amp;run=run&amp;token=p.eyJ1IjogIjdmOTIwMmMzLWM1ZjctNDU4Ni1hZDUxLTdmYzUzNTRlMTk5YSIsICJpZCI6ICI3N2NiMDljOS1hYjVjLTQ2MzYtYTA4Yi00ZDYwNDQwMzUyZTYifQ.XhZPypxjhaeJbdJzNozOKmKPbFqM9CWkAil8WiXFaAE"></iframe></figure>
