09Q71AO534
09Q71AO534

Reputation: 4440

How Can i cast the Date datatype into integer?(postgresql)

I want the no of days into a column which is defined as integer in a table i.e.,

select age('2013-04-06','2013-04-04') --> gives me 2days as output

i want that 2 days to be stored into a column with datatype integer.. i tried this but i am getting the no of hours from this query..

SELECT (EXTRACT(epoch FROM (select age('2013-07-06','2013-07-04')))/3600); --> 48 i.e., 48 hours as output

i need the integer value(2) from the 2 days or from the 48 hours

How to get this to be done?

Upvotes: 1

Views: 5467

Answers (2)

09Q71AO534
09Q71AO534

Reputation: 4440

select '2014-05-02'::date - '2013-04-01'::date;

gives the No of days even its difference between day, month or year

Upvotes: 2

Craig Ringer
Craig Ringer

Reputation: 324375

age(...) produces an interval. You can then use extract on the interval to get the days:

select extract(day from age('2013-04-06','2013-04-04'));

With some inputs you'd want to justify_interval the interval before calling extract on it - but age produces a pre-justified interval, so that shouldn't be needed in this case.

Upvotes: 4

Related Questions