Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to get the difference between two timestamps in days with decimals.

i.e. instead of interval, 3 days 20:33:54.937, the desired output is 3.85 (in numeric format)

+---------------------+---------------------+--+
|        date1        |      date_2         |  |
+---------------------+---------------------+--+
| 2020-12-22 08:10:11 | 2020-12-18 11:36:16 |  |
+---------------------+---------------------+--+

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

demo:db<>fiddle

You can use EXTRACT(epoch FROM your_interval) which returns the seconds of the interval. These can be converted into days:

SELECT EXTRACT(
    epoch FROM (
      '2020-12-22 08:10:11'::timestamp - '2020-12-18 11:36:16'::timestamp
    )
)/3600/24

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...