Oracle:使用where子句按月选择日期

时间:2019-05-28 08:37:40

标签: sql oracle

我正在为交易应用程序开发一个oracle数据库。我在选择日期和月份时遇到了这个问题。我确实知道这在PL / SQL中是可以实现的,但是我觉得必须有一种仅使用oracle语句的方法。

根据我的研究,以下声明是从表的每一行中选择月份。

select to_char(sysdate, 'Month') from income
我试图根据自己的需要修改它,但是我失败了。 到目前为止,我已经提出了这个声明,但是它不起作用

select * from income where to_char(sysdate,month) = 'feb'

例如, 我将能够选择“ 2012年5月”,而不是显示所有记录。以前有没有人碰过这个问题?

预先感谢您:)

3 个答案:

答案 0 :(得分:1)

您在追寻类似的东西吗?

select *
from   income
where  <date_column> >= to_date('01/05/2019', 'dd/mm/yyyy')
and    <date_column> <  to_date('01/06/2019', 'dd/mm/yyyy')

(用您要过滤的收入表中的日期列的名称替换<date_column>)?

答案 1 :(得分:0)

我认为您可以使用以下查询:

select *
from   income
where  to_char(<date_column>,'MON-RRRR') = 'MAY-2019';

答案 2 :(得分:0)

如果您想传递诸如'May 2012'之类的字符串,那么我建议:

select i.*
from income i
where i.datecol >= to_date('May 2012', 'Mon YYYY') and
      i.datecol < to_date('May 2012', 'Mon YYYY') + interval '1' month;

也就是说,我认为您的应用程序应该将字符串值转换为日期范围,并且应该在查询中使用该范围:

select i.*
from income i
where i.datecol >= :datestart
      i.datecol < :dateend + interval '1 day';

我强烈建议您避免使用between加上日期,尤其是在Oracle中。 date数据类型具有内置的时间成分,可能会导致比较失败。

相关问题