Oracle SQL Current_Date

时间:2018-05-14 17:05:44

标签: sql oracle sysdate

我在简单查询中使用Current_Date函数时遇到了一些问题,但我无法弄清楚原因。我正在使用Oracle SQL Developer 3.2在Oracle 12c环境中工作。

我的原始查询看起来像这样:

select * from Inventory where Placement_End_Dt >= Current_date

上述工作正常,但它没有获取Placement_End_Dt今天(5月14日18日)的记录

我试图按如下方式简化查询,但这也不会返回任何内容

select * from Inventory where Placement_End_Dt = Current_date

但是,当我按如下方式应用日期格式时,它可以工作:

select * from Inventory where to_char(Placement_End_Dt, 'DD-MM-YYYY') = to_char(Current_date, 'DD-MM-YYYY')

然后我尝试对此进行扩展以恢复到我的原始查询,以选择具有从今天开始的结束日期的所有记录:

select * from Inventory where to_char(Placement_End_Dt, 'DD-MM-YYYY') => to_char(Current_date, 'DD-MM-YYYY')

这失败了,因为它选择了Placement_End_Dt过去,现在和未来的记录!

Placement_End_Dt列定义为Oracle DATE数据类型

对于如何让这个查询起作用,我将不胜感激。

3 个答案:

答案 0 :(得分:5)

使用to_char时,您正在比较字符串。

to_char(date '2000-01-20', 'DD-MM-YYYY') > to_char(date '2018-05-14', 'DD-MM-YYYY')

因为' 20-01-2000'因为字符串中的第一个字母大于' 14-05-2018',' 2' > ' 1'

并且CURRENT_DATE几乎从未使用过,因为它使用了您计算机的时间,而不是数据库时间,因此您可以轻松地休息几个小时。请改用SYSDATE

答案 1 :(得分:0)

试试这个,它将返回当前和未来几天的行。

select * from Inventory where trunc(Placement_End_Dt) >= trunc(sysdate);

答案 2 :(得分:0)

我建议使用此查询

select * from Inventory where trunc(Placement_End_Dt) = trunc(sysdate);

Oracle Date列默认情况下也存储时间戳,因此除非记录与第二个记录相同,否则它们不会匹配。在日期列上使用trunc()时,它会截断时间戳并仅保留日期。

相关问题