S
S
Sergei Iamskoi2016-04-19 15:31:39
PostgreSQL
Sergei Iamskoi, 2016-04-19 15:31:39

How to get date as a percentage in Postgresql?

We switched to postgres, and some things that were easy to do in MySQL are not as easy to do in PostgreSQL.
There are two fields with datetime types (timestamp without gmt) - start date and end date. It is necessary to get as a percentage how much is left to reach the final date from the current time. In MySQL, this can be done with the following query:

select ((end_date - now()) * 100) / (end_date - begin_date) from table

The result will be how many percent completed.
How to do it in postgres?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Niomin, 2016-04-19
@syamskoy

select (EXTRACT(epoch FROM end_date - now()) * 100) / EXTRACT(epoch FROM end_date - begin_date) from table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question