rahul kumar
rahul kumar

Reputation: 77

select where sum of time column & integer column is less than current_time

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

Answers (1)

mu is too short
mu is too short

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. times 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

Related Questions