oracle根据Date字段查询区间数据(转)

时间:2024-03-04 17:22:30

    用过Oracle数据库的朋友应该知道,Oracle数据库在以Date类型为查询条件时存在一个小小的BUG,如:

    select * from tableName where createDate >= to_date(\'2007-01-01\',\'yyyy-mm-dd\') and createDate <= to_date(\'2007-01-01\',\'yyyy-mm-dd\');

   【tableName:表名 ;createDate:表tableName的一个列名,为Date类型 】

    如果我们希望通过上面的语句来查询createDate为2007-01-01当天的记录的话,很遗憾,不管那天有没有数据产生,我们得到的结果都为空,也就是说Oracle数据库在查询时间段内的记录时,记录中不包括截止日期当天所产生的数据,但是很明显我们需要得到包括截止日期当天所产生的数据,只要稍作处理即可:

    select * from tableName where createDate >= to_date(\'2007-01-01\',\'yyyy-mm-dd\') and createDate <= (to_date(\'2007-01-01\',\'yyyy-mm-dd\')+1);