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

Categories

0 votes
1.5k 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...