System: Postgresql 9.6. I didn't design this system and this table has startdate (Date type field), starttime (Time), enddate (Date) and endtime (Time) fields but not hours elapsed between the 2. The field types for each field above are in (parens).
What I need: using Date and Time fields, I would like the difference in decimal hours. This would be a calculated column in my SELECT statement.
I tried this and got the result in "hh:mm:ss" format:
endtime - starttime as hrs
I've been searching a while how to convert that result to decimal hours. I've also tried these formats:
(date enddate time endtime) - (date startdate time starttime) as hrs
(enddate endtime) - (startdate starttime) as hrs
timestamp(enddate endtime) - timestamp(startdate starttime) as hrs
age(enddate endtime) - age(startdate starttime) as hrs
extract(EPOCH FROM timestamptz(enddate endtime)) - extract(EPOCH from timestamptz (startdate starttime))
I don't do much SQL at my job so any help would be appreciated. I learn by seeing code but I'd also like to understand what each part does so I can UNDERSTAND and apply that understanding to future issues.
Thank you!
select extract (EPOCH from (enddate + endtime - (startdate + starttime))) / (60 * 60) from a;