postgres compare now (time only) to a time (time only now)

Viewed 6097

I have a column that is time without time zone (24 hour time), i.e. 11:30:10 (11:30 AM and 10 seconds).

How would I compare this with now, i.e. if this columns is greater than now (returning true if queried after 11 AM).

Something like:

select * from table where time_column < date_part(now(), 'HH:MM:SS');
3 Answers

I think something wrong in the current answer.

because to_timestamp(to_char(now(),'HH:MM:SS'),'HH:MM:SS')::time

postgresql document says MM is the month range [0-12] not minutes range also in the database time column you should have 24 hour time format so here again you should use. HH24

select  to_timestamp(to_char(now(),'HH24:MI:SS'),'HH24:MI:SS')::time;

For comparing time use current_time built in function. this function returns now time with timezone. if you want specific timezone please set it before.

select * from table where time_column < current_time::time;
select * from table where current_time::time between start_time_colum and end_time_column;
Related