How to Query Date and Time

If you want to obtain data from any specific date, just put the date in the format ‘mm/dd/yyyy’. If the column data type is timestamp, you can cast to date with the cast function.

select * from sales where cast(sold_at as date) = ‘01/02/2021’; -- format mm/dd/yyyy

If you want to obtain the sales done on each day in a specific period of time, for example between 11:00 am and 12:00 am, you can cast the timestamp to time and filter in a range of time. Then we can group by date.

select cast(sold_at as date), sum(revenue) 
from   sales 
where  cast(sold_at as time) between '11:00' AND '12:00'
group by cast(sold_at as date) 
order by 1;

IN THIS PAGE