Oracle 比较时间戳和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12609487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:30:53  来源:igfitidea点击:

Oracle comparing timestamp with date

oracle

提问by user1050619

I have a timestamp field and I just want to compare the date part of it in my query in Oracle

我有一个时间戳字段,我只想在 Oracle 查询中比较它的日期部分

How do I do that,

我怎么做,

SELECT *
FROM Table1
WHERE date(field1) = '2012-01-01' 

回答by beny23

You can truncatethe date part:

您可以截断日期部分:

select * from table1 where trunc(field1) = to_date('2012-01-01', 'YYYY-MM-DD')

The trouble with this approach is that any index on field1wouldn't be used due to the function call.

这种方法的问题在于,field1由于函数调用,不会使用任何索引。

Alternatively (and more index friendly)

或者(对索引更友好)

select * from table1 
 where field1 >= to_timestamp('2012-01-01', 'YYYY-MM-DD') 
   and field1 < to_timestamp('2012-01-02', 'YYYY-MM-DD')

回答by hol

You can truncate the date

您可以截断日期

SELECT *
FROM Table1
WHERE trunc(field1) = to_Date('2012-01-01','YYY-MM-DD')

Look at the SQL Fiddlefor more examples.

查看SQL Fiddle以获取更多示例。

回答by Tolga ?EL?K

to_dateformat worked for me. Please consider the date formats: MON-, MM, ., -.

to_date格式对我有用。请考虑日期格式: MON-, MM, ., -

t.start_date >= to_date('14.11.2016 04:01:39', 'DD.MM.YYYY HH24:MI:SS')
t.start_date <=to_date('14.11.2016 04:10:07', 'DD.MM.YYYY HH24:MI:SS')