Oracle比较时间戳和日期
问题描述:
我有一个时间戳字段,我只想在Oracle查询中比较它的日期部分
I have a timestamp field and I just want to compare the date part of it in my query in Oracle
我该怎么做
SELECT *
FROM Table1
WHERE date(field1) = '2012-01-01'
答
您可以截断日期部分:
select * from table1 where trunc(field1) = to_date('2012-01-01', 'YYYY-MM-DD')
这种方法的麻烦之处在于,由于函数调用,field1
上的任何索引都不会被使用.
The trouble with this approach is that any index on field1
wouldn't be used due to the function call.
或者(对索引更友好)
select * from table1
where field1 >= to_timestamp('2012-01-01', 'YYYY-MM-DD')
and field1 < to_timestamp('2012-01-02', 'YYYY-MM-DD')