sqlpostgresqltimestampdate-range

How to filter data in timestamp range, with specific date/time from and to?


Assuming, I have data as below:

2021-09-10  09:00:00.000+00
2021-09-20  10:00:00.000+00
2021-09-20  11:00:00.000+00

I want to filter date/timestamp range from 2021-09-20 10am to 2021-09-21 6pm.

How can I write query to get it?


Solution

  • You need a range condition in the WHERE clause:

    select *
    from the_table
    where the_column >= timestamp '2021-09-20 10:00:00'
      and the_column  < timestamp '2021-09-21 18:00:00'
    

    timestamp literals are specified using the ISO standard for date/time literals. So the time part needs to be written in 24hour format.

    It's not clear from your question if you want to include rows at 18:00:00 or if that should be the upper bound which is excluded. The above assumes the latter.