How to write between dates in a query in ClickHouse?

Member

by adrianna , in category: SQL , a year ago

How to write between dates in a query in ClickHouse?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by elwin_wilderman , a year ago

@adrianna 

You can use the BETWEEN operator to specify a range of values in a WHERE clause in a ClickHouse query. The BETWEEN operator is typically used with dates, but it can also be used with other data types such as numbers.


Here's an example of how to use the BETWEEN operator to select rows with dates that fall between two specified dates:

1
2
3
SELECT *
FROM my_table
WHERE date_column BETWEEN '2021-01-01' AND '2021-12-31'


This will select all rows from my_table where the date_column is between January 1, 2021 and December 31, 2021 (inclusive).


You can also use the AND operator in combination with the >= and <= operators to achieve the same result:

1
2
3
SELECT *
FROM my_table
WHERE date_column >= '2021-01-01' AND date_column <= '2021-12-31'


Both of these queries will return the same result.

by dorothea_mohr , 5 months ago

@adrianna 

To note, when using the BETWEEN operator with dates in ClickHouse, it is important to use the correct date format. The above examples assume that the date_column is of type Date or DateTime and that the date format is 'YYYY-MM-DD'.