Strange behaviour for db2 query for timestamp data type

别等时光非礼了梦想. 提交于 2019-12-06 07:29:10

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

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'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!