Time stamp issue
Hi Everyone,
I have a table named concurrent_user which has a column time_stamp. The
column stores the timestamp for the latest entered record.
My query finds the difference of the timestamp from the current time, if
the value is larger than 5 minutes then the latest entered record in the
table is assigned to a new user if he tries through the application else
he is displayed an error message.
The thing worked fine while the database was on Oracle but since
migrating it to postgres it is not able to handle the conversion.
The query is something like :
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP) >
?
Here the calculated value in '?' is not supported by the postgres as it
was set as a double.
I tried to cast it to a timestamp by using Timestamp timestamp = new
Timestamp((java.sql.Date(rs.getTimestamp(time_stamp)).getTime());
But then it started to give :
...... <Exception Message> : ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00" -
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00"
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00"
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecu
torImpl.java:1548)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImp
l.java:1316)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:
191)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Stateme
nt.java:452)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdb
c2Statement.java:351)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2S
tatement.java:305)
at
org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeUpdate(Ca
chedPreparedStatement.java:95)
at
org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(W
rappedPreparedStatement.java:251)
Kindly help for the same
Regards,
Kapil
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
Difference between timestamps will give you an interval. So the LHS of
">" is interval and hence the RHS should be an interval too. You should
not be converting the right hand side value to Timestamp. What is the
"double" value you are calculating? Is it in minutes, hours or days?
I am not a Java expert, but the effective query should look as follows
delete from concurrent_user where (current_timestamp -
concurrent_user.time_stamp) > interval '5 minutes';
-Chandu
________________________________
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of
kapil.munish@wipro.com
Sent: Tuesday, January 08, 2008 8:52 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Time stamp issue
Importance: High
Hi Everyone,
I have a table named concurrent_user which has a column time_stamp. The
column stores the timestamp for the latest entered record.
My query finds the difference of the timestamp from the current time, if
the value is larger than 5 minutes then the latest entered record in the
table is assigned to a new user if he tries through the application else
he is displayed an error message.
The thing worked fine while the database was on Oracle but since
migrating it to postgres it is not able to handle the conversion.
The query is something like :
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP) >
?
Here the calculated value in '?' is not supported by the postgres as it
was set as a double.
I tried to cast it to a timestamp by using Timestamp timestamp = new
Timestamp((java.sql.Date(rs.getTimestamp(time_stamp)).getTime());
But then it started to give :
...... <Exception Message> : ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00" -
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00"
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type
interval: "1970-01-01 01:05:00.000000 +01:00:00"
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecu
torImpl.java:1548)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImp
l.java:1316)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:
191)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Stateme
nt.java:452)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdb
c2Statement.java:351)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2S
tatement.java:305)
at
org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeUpdate(Ca
chedPreparedStatement.java:95)
at
org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(W
rappedPreparedStatement.java:251)
Kindly help for the same
Regards,
Kapil
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain proprietary, confidential or privileged information. If
you are not the intended recipient, you should not disseminate,
distribute or copy this e-mail. Please notify the sender immediately and
destroy all copies of this message and any attachments. WARNING:
Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The
company accepts no liability for any damage caused by any virus
transmitted by this email. www.wipro.com
On Jan 8, 2008 8:51 AM, <kapil.munish@wipro.com> wrote:
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP) > ?
Here the calculated value in '?' is not supported by the postgres as it was
set as a double.I tried to cast it to a timestamp by using Timestamp timestamp = new
Timestamp((java.sql.Date(rs.getTimestamp(time_stamp)).getTime());
When you subtract one timestamp from another, you get an interval, not
a timestamp. I.e. now() - (now() - 5 minutes) gives a result of 5
minutes.
So, you need to replace the ? with 5 minutes, so your query looks
something like this:
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP)
interval '5 minutes'
or this:
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP)
'5 minutes'::interval
or this:
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP)
Show quoted text
cast('5 minutes' as interval)
On Tue, Jan 08, 2008 at 08:21:40PM +0530, kapil.munish@wipro.com wrote:
The query is something like :
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP) >
?Here the calculated value in '?' is not supported by the postgres as it
was set as a double.
In postgres subtracting two timestamps produces an interval. If you
want to pass your parameter in seconds, try:
DELETE from CONCURRENT_USER WHERE (now() - CONCURRENT_USER.TIME_STAMP) > (? * '1 second'::interval)
Or if the use of indexes is important to you:
DELETE from CONCURRENT_USER WHERE CONCURRENT_USER.TIME_STAMP < (now() - (? * '1 second'::interval));
Perhaps JDBC can handle intervals itself also, that I don't know.
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
Show quoted text
Those who make peaceful revolution impossible will make violent revolution inevitable.
-- John F Kennedy