0

我有一个 oracle DATE 列,该列的特定记录的值是

16/10/2005 11:13:34 AM.

现在我正在放入一个像这样的过滤器,过滤器字符串是从前端传入的。

date_col between to_date('16-10-2005','DD-MM-YYYY') and to_date('16-10-2005','DD-MM-YYYY') 

它返回零记录。为什么会这样?我在想 oracle 会返回所有日期为 16/10/2005 的记录,无论时间如何。我是否还需要传递时间/从前端,我只得到日期而不是时间。

4

3 回答 3

2
date_col between to_date('16-10-2005','DD-MM-YYYY') 
     and to_date('16-10-2005','DD-MM-YYYY') 

只会返回DATE2005 年 10 月 16 日午夜的值。如果您想获取 2005 年 10 月 16 日任何时间的数据

date_col between to_date('16-10-2005','DD-MM-YYYY') 
             and to_date('16-10-2005 23:59:59','DD-MM-YYYY HH24:MI:SS') 

会做的。也会

date_col between to_date('16-10-2005','DD-MM-YYYY') 
             and to_date('17-10-2005','DD-MM-YYYY') - interval '1' second

如果您未能减去那一秒,您最终还将拉date_col取值为 2005 年 10 月 17 日午夜的行。

您还可以将trunc功能应用于您的date_col

trunc(date_col) = date '2005-10-16'

但这会阻止使用标准索引date_col。您通常需要在trunc(date_col)

CREATE INDEX idx_trunc_date_col
    ON table_name( trunc(date_col) );
于 2012-08-20T20:10:46.707 回答
2

是的 - 你需要一个时间元素。

to_date('16-10-2005','DD-MM-YYYY') 默认为午夜 - 因此要检索 16/10/05 的所有记录,您可以使用

date_col >= to_date('16-10-2005','DD-MM-YYYY')
and date_col < to_date('17-10-2005','DD-MM-YYYY')
于 2012-08-20T20:10:53.860 回答
1

将第二天的最后一个日期减去 1 秒...

date_col between to_date('16-10-2005','DD-MM-YYYY') 
    and to_date('16-10-2005 23:59:59','DD-MM-YYYY HH:MI:SS')
于 2012-08-20T20:08:47.747 回答