views:

175

answers:

3

How to write a query to find the time difference ?

time format is like this 2009-08-12 02:59:59

i want to compare this time with

2009-08-12 02:59:10

how to check these two

i want to return some row having the time difference is 30sec

how to write a SQL statement ??

+1  A: 
SELECT * FROM your_table WHERE time1_column - time2_column = interval '30s'

Sorry this is the best I can do, given your description of the problem...

maksymko
I believe you will have to allow for the interval being '-30s' also, in the case where time2_column is greater than time1_column.
Paul
A: 

if both the times are columns in database table, you can directly use relational operators (>, < and =) Ex. if you have a table like Test ( id bigint, starttime timestamp, endtime timestamp );

then you can have queries like select * from test where starttime > end time etc..

If you want to compare two dates in query, you can first convert text to time and then compare them you can use: datetime function of pgsql see: http://www.postgresql.org/docs/6.3/static/c10.htm for more details

Vineyard
+2  A: 
select date_part('second',date1) - date_part('second',date2)

In SQL you can do like this which give you output in seconds

KuldipMCA
I think it does not work well, as it only works in the same minute.The sentence will return 0 if you compare 2009-08-12 02:59:59 and 2012-10-08 12:13:59
Guido