Reputation: 77
I have two columns duration(integer) and start_time(time).
I have to select where sum of start_time and duration is greater than current_time
.
Something Like this -
Availability.where("start_time + duration < current_time")
How to do this? I am using Postgres in Ruby-on-Rails
Upvotes: 1
Views: 56
Reputation: 434606
To add an integer
to a time
, you'd probably want to convert the integer
to an interval
by specifying the units and casting. If the duration
is in minutes then:
start_time + (duration || ' minutes')::interval > current_time
if the duration
is seconds then:
start_time + (duration || ' seconds')::interval > current_time
Note that ||
is the string concatenation operator in SQL.
You can put one of those into a where
call:
Availability.where("start_time + (duration || ' seconds')::interval > current_time")
Keep in mind that '23:00'::time + '2 hours'::interval
is 01:00:00
(i.e. time
s wrap around at midnight) so you might need to include a date component depending on what the data looks like and what the specific need is.
Upvotes: 3