How do I convert an interval into a number of hours with postgres?

agnul picture agnul · Jun 4, 2009 · Viewed 159.1k times · Source

Say I have an interval like

4 days 10:00:00

in postgres. How do I convert that to a number of hours (106 in this case?) Is there a function or should I bite the bullet and do something like

extract(days, my_interval) * 24 + extract(hours, my_interval)

Answer

Magnus Hagander picture Magnus Hagander · Jun 4, 2009

Probably the easiest way is:

SELECT EXTRACT(epoch FROM my_interval)/3600