小于或等于 Oracle SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2996915/
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 02:37:27  来源:igfitidea点击:

LESS THAN OR EQUAL TO IN Oracle SQL

oracleplsql

提问by Andromeda

updated_date  = 08-Jun-2010;

I have a query like this

我有一个这样的查询

select * from asd whre updated_date <= todate('08-Jun-2010', 'dd-MM-yy');

but I am not getting any results. it only works if todateis 09-Jun-2010...

但我没有得到任何结果。它仅适用todate于 2010 年 6 月 9 日...

i.e. my equaltooperator is not working properly.

即我的equalto操作员工作不正常。

why is it like that?

为什么会这样?

回答by Vincent Malgrat

In Oracle a DATE is a point in time. It always has a time component with a precision to the second. todate('08-Jun-2010', 'dd-Mon-yyyy')is in Oracle the same as todate('08-Jun-2010 00:00:00', 'dd-Mon-yyyy hh24:mi:ss'). So if you select rows up to that date you won't get any row on that day with a time component not equal to 00:00.

在 Oracle 中,日期是一个时间点。它总是有一个精确到秒的时间分量。todate('08-Jun-2010', 'dd-Mon-yyyy')在 Oracle 中与todate('08-Jun-2010 00:00:00', 'dd-Mon-yyyy hh24:mi:ss'). 因此,如果您选择截至该日期的行,则当天不会获得任何时间分量不等于 的行00:00

If you want to select all the rows up to and including 08-JUN-2010, I would suggest using:

如果您想选择直到并包括 的所有行08-JUN-2010,我建议使用:

< to_date('09-06-2010', 'dd-MM-yyyy')

or

或者

<= to_date('08-06-2010 23:59:59', 'dd-MM-yyyy hh24:mi:ss')

Note- I corrected your date format: you need to use MONif you want to use the abbreviated month name. I would suggest using MMinstead, so that you won't get error when someone changes its client settings (NLS_DATE_LANGUAGE). Also prefer the use of YYYYinstead of YY.

注意- 我更正了您的日期格式:MON如果您想使用缩写的月份名称,则需要使用。我建议MM改为使用,这样当有人更改其客户端设置 ( NLS_DATE_LANGUAGE)时您就不会出错。也更喜欢使用YYYY代替YY

回答by Sujee

Check this,

检查这个,

select to_date('08-Jun-2010', 'dd-MON-yyyy') from dual;

It is equal to 2010-06-08 00:00:00. Notice the time.

它等于2010-06-08 00:00:00。注意时间。

The updated_datehas time portion. To include them, please use this query,

所述updated_date具有时间部分。要包含它们,请使用此查询,

select * from asd where trunc(updated_date) <= to_date('08-Jun-2010', 'dd-MON-yyyy');

The default TRUNCfunction for date parameter will remove the time.

TRUNCdate 参数的默认函数将删除时间。

Refer Trunc

参考中继