Reputation: 433
I want to query for timestamp data type in db2. I wrote query below
Select * from sample where LASTMODIFIEDDATE = timestamp('2012-04-03 07:59:50')
I didn't get any result for above query , then I tried
Select * from sample where LASTMODIFIEDDATE > timestamp('2012-04-03 07:59:50')
In above query I got results matching timestamp '2012-04-03 07:59:50' plus for greater values of timestamp, e.g '2012-04-03 08:59:50'.
If I am getting results for '>' operator then why not I am not getting any results for '=' operator ? Any reasons or am I writing wrong query ?
Thanks !
Upvotes: 2
Views: 4888
Reputation: 1044
You could also truncate the db column's value to seconds alone and then perform the comparison with your string:
Select * from sample where TRUNC(LASTMODIFIEDDATE, 'SS') = '2012-04-03 07:59:50'
Works in DB2 10.5. You could also truncate by:
hour ('HH'), minute('MI'), year('YEAR' or 'YYYY'), month('MONTH' or 'MM'), Day ('DD')
Example:
Select * from sample where TRUNC(LASTMODIFIEDDATE, 'HH') = '2012-04-03 07:00:00'
Select * from sample where TRUNC(LASTMODIFIEDDATE, 'MI') = '2012-04-03 07:59:00'
Upvotes: 0
Reputation: 13056
No, DB2 stores the full value of the timestamp, including the fractional seconds. You may wish to change the format the system displays timestamps in to something that includes milliseconds.
Try using this instead:
SELECT *
FROM Sample
WHERE lastModifiedDate >= TIMESTAMP('2012-04-03 07:59:50')
AND lastModifiedDate < TIMESTAMP('2012-04-03 07:59:50)' + 1 SECONDS
Unless you have the full value of the timestamp, including milliseconds, you're going to be getting a range - when accessing a range of data, use 'lower-bound inclusive, upper-bound exclusive'.
Upvotes: 6