C. S. F. Junior
C. S. F. Junior

Reputation: 341

Calculate the difference between two hours on PostgreSQL 9.4

Given a table as the following:

    create table meetings(
    id integer primary key,
    start_time varchar,
    end_time varchar
)

Considering that the string stored in this table follow the format 'HH:MM' and have a 24 hours format, is there a command on PostgreSQL 9.4 that I can cast fields to time, calculate the difference between them, and return a single result of the counting of full hours available?

e.g: start_time: '08:00' - end_time: '12:00' Result must be 4.

Upvotes: 1

Views: 80

Answers (1)

Victor
Victor

Reputation: 3978

In your particular case, assuming that you are working with clock values (both of them belonging to the same day), I would guess you can do this

(clock_to::time - clock_from::time) as duration

Allow me to leave you a ready to run example:

with cte as (
  select '4:00'::varchar as clock_from, '14:00'::varchar as clock_to
)
select (clock_to::time - clock_from::time) as duration
from cte

Upvotes: 1

Related Questions