1

I have 2 "timestamp" columns in a table, in a Postgres database. I need to extract the difference in days between these 2 columns. Let's consider the columns are "column1" and "column2". And the difference between these 2 timestamps is 10 hours. So, for the query:

select column2 - column1 from table

The result is of "interval" type, and it is "10:00:00". I need to convert this in a real number, and, for this example, to obtain the result as something like "0.41666...7"

2
  • I need to convert this in a real number I am not sure what you mean? For example what would be the value for 10:32:41? Commented Oct 15, 2015 at 10:55
  • "real number" - equivalent to a "double" value in Java Commented Oct 15, 2015 at 11:17

2 Answers 2

3

You can get the number of seconds from the resulting interval using extract()

select extract(epoch from column2 - column1)
from the_table;

If you divide that by 86400 to get it as a fraction of a day

Sign up to request clarification or add additional context in comments.

Comments

2
DATE_PART('day', column2 - column1)

http://www.sqlines.com/postgresql/how-to/datediff

3 Comments

This returns an integer value. And I need a value with decimals. With date_part, the result is "0".
See the reference I linked. DATE_PART('hour', end - start )/24 should give you the extra fractional part of a day. So whole answer would be DATE_PART('day', column2 - column1) + DATE_PART('hour', end - start )/24
This seems like it might look purely at the minute, without consideration of the other denominators e.g. for DATE_PART('minute', time_1 - time_2) < 1, I'm getting time_1 = 2019-06-21 09:32:45.073002 and time_2 = 2019-06-24 04:32:47.779387 returned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.