Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

postgresql - Timestamp difference in numeric

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 |  |
+---------------------+---------------------+--+

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...