Postgres where clause compare timestamp

前端 未结 1 1988
后悔当初
后悔当初 2021-01-30 03:37

I have a table where column is of datatype timestamp

Which contains records multiple records for a day I want to select all rows corresponding to day

相关标签:
1条回答
  • 2021-01-30 04:38

    Assuming you actually mean timestamp because there is no datetime in Postgres

    Cast the timestamp column to a date, that will remove the time part:

    select *
    from the_table
    where the_timestamp_column::date = date '2015-07-15';
    

    This will return all rows from July, 15th.

    Note that the above will not use an index on the_timestamp_column. If performance is critical, you need to either create an index on that expression or use a range condition:

    select *
    from the_table
    where the_timestamp_column >= timestamp '2015-07-15 00:00:00'
      and the_timestamp_column < timestamp '2015-07-16 00:00:00';
    
    0 讨论(0)
提交回复
热议问题