Question

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 ??

Was it helpful?

Solution

select date_part('second',date1) - date_part('second',date2) 

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

OTHER TIPS

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...

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top