PostgreSQL Timestamp substraction - want the result in hours
Table :
create table examp (
I开发者_如何转开发D integer,
val1 timestamp,
val2 timestamp,
primary key(ID)
);
insert into examp (ID,val1,val2) values (1,'2011/5/26 10:00:00','2011/5/26 14:00:00');
insert into examp (ID,val1,val2) values (2,'2011/5/26 14:00:00','2011/5/25 14:00:00');
Now I want to know the difference of two time stamps in Hours> I tried:
SELECT EXTRACT(hour FROM AGE(val1,val2))
FROM examp
WHERE id = 2
...but that gives me 0!
You are extracting the hour component, not the number of hours. Since the timestamp is 24 hours apart, the difference of the hour field is indeed zero.
Presumably you can extract days * 24 + extract hours
to get the information you want. As long as it isn't months or years apart... You could also convert to epoch and then / 3600.
select extract(epoch from age(val1,val2))/3600 from examp where id=2;
精彩评论