Oracle 选择今天之间的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25084458/
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
Oracle Select Where Date Between Today
提问by Felipe M
I have a Oracle SELECT Query like this:
我有一个像这样的 Oracle SELECT 查询:
Select * From Customer_Rooms CuRo
Where CuRo.Date_Enter Between 'TODAY 12:00:00 PM' And 'TODAY 11:59:59 PM'
I mean, I want to select all where the field "date_enter" is today. I already tried things like Trunc(Sysdate) || ' 12:00:00'
in the between, didn't work.
我的意思是,我想选择今天“date_enter”字段所在的所有位置。我已经尝试过Trunc(Sysdate) || ' 12:00:00'
介于两者之间的事情,但没有奏效。
Advice:I can't use TO_CHAR because it gets too slow.
建议:我不能使用 TO_CHAR,因为它太慢了。
回答by Alex Poole
Assuming date_enter
is a DATE
field:
假设date_enter
是一个DATE
字段:
Select * From Customer_Rooms CuRo
Where CuRo.Date_Enter >= trunc(sysdate)
And CuRo.Date_Enter < trunc(sysdate) + 1;
The trunc()
function strips out the time portion by default, so trunc(sysdate)
gives you midnight this morning.
该trunc()
函数默认去掉时间部分,所以trunc(sysdate)
今天早上给你午夜。
If you particularly want to stick with between
, and you have a DATE
not a TIMESTAMP
, you could do:
如果您特别想坚持使用between
,并且您有一个DATE
not TIMESTAMP
,则可以执行以下操作:
Select * From Customer_Rooms CuRo
Where CuRo.Date_Enter between trunc(sysdate)
And trunc(sysdate) + interval '1' day - interval '1' second;
between
is inclusive, so if you don't take a second off then you'd potentially pick up records from exactly midnight tonight; so this generates the 23:59:59 time you were looking for in your original query. But using >=
and <
is a bit clearer and more explicit, in my opinion anyway.
between
是包容性的,所以如果你不休息一下,那么你可能会从今晚的午夜开始获得记录;所以这会生成您在原始查询中寻找的 23:59:59 时间。但无论如何,在我看来,使用>=
and<
会更清晰、更明确。
If you're sure you can't have dates later than today anyway, the upper bound isn't really adding anything, and you'd get the same result with just:
如果您确定无论如何都不能有比今天晚的日期,则上限实际上并没有添加任何内容,并且您将获得相同的结果:
Select * From Customer_Rooms CuRo
Where CuRo.Date_Enter >= trunc(sysdate);
You don't want to use trunc
or to_char
on the date_enter
column though; using any function prevents an index on that column being used, which is why your query with to_char
was too slow.
但是,您不想在列上使用trunc
或;使用任何函数都会阻止使用该列的索引,这就是您的查询速度太慢的原因。to_char
date_enter
to_char
回答by PedroFTW
In my case, I was searching trough some log files and wanted to find only the ones that happened TODAY.
就我而言,我正在搜索一些日志文件,只想找到今天发生的那些。
For me, it didn't matter what time it happened, just had to be today, so:
对我来说,它发生在什么时间并不重要,只需要今天,所以:
/*...*/
where
trunc(_DATETIMEFROMSISTEM_) = trunc(sysdate)
It works perfectly for this scenario.
它非常适合这种情况。
回答by Harendra Singh Rawat
Yes, you can write query like this
是的,你可以这样写查询
select * from table_name where last_time<=CURRENT_TIME() and last_date<=CURRENT_DATE()
回答by prashant thakre
Try the below code
试试下面的代码
Select * From Customer_Rooms CuRo
Where trunc(CuRo.Date_Enter) = 'TODAY' -- TODAY is the date of today without any timestamp.