[db2] DB2 Timestamp select statement

I am trying to run a simple query which gets me data based on timestamp, as follows:

SELECT * 
FROM <table_name> 
WHERE id = 1 
AND usagetime = timestamp('2012-09-03 08:03:06') 
WITH UR;

This does not seem to return a record to me, whereas this record is present in the database for id = 1.

What am I doing wrong here?

The datatype of the column usagetime is correct, set to timestamp.

This question is related to db2 timestamp

The answer is


You might want to use TRUNC function on your column when comparing with string format, so it compares only till seconds, not milliseconds.

SELECT * FROM <table_name> WHERE id = 1 
AND TRUNC(usagetime, 'SS') = '2012-09-03 08:03:06';

If you wanted to truncate upto minutes, hours, etc. that is also possible, just use appropriate notation instead of 'SS':

hour ('HH'), minute('MI'), year('YEAR' or 'YYYY'), month('MONTH' or 'MM'), Day ('DD')