简体   繁体   English

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

[英]Oracle : Selecting date by month with where clause

I came across a problem that in selecting the date for current desired month and year. 我在选择当前所需月份和年份的日期时遇到了一个问题。 I tried the 2 statements shown below but failed to execute the query 我尝试了下面显示的2条语句,但是无法执行查询

select to_char(sysdate, 'Month') from income

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

Update 更新资料

But after researching and learning more in depth on oracle docs website. 但是在oracle docs网站上进行了更深入的研究和学习之后。 What i came out with is to use "between" clause. 我想到的是使用“ between”子句。 Specifying the first day and last day of the month . 指定每月的第一天和最后一天。 Doing so, it will execute the desired month/year 这样,它将执行所需的月份/年份

For an example 举个例子

SELECT column_name
FROM table_name where column_name = (Your own value) AND
column_date >= to_date('01/02/2012', 'dd/mm/yyyy')
and column_date <  to_date('01/03/2012', 'dd/mm/yyyy')  

I hope this help :) 我希望这个帮助:)

Are you after something like: 您是否喜欢以下内容:

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')

(replacing <date_column> with the name of the date column in your income table that you want to filter on)? (用要过滤的收入表中的日期列名称替换<date_column> )?

I think you can use the following query: 我认为您可以使用以下查询:

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

If you want to pass in a string like 'May 2012' , then I would recommend: 如果您想传递'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;

That said, I think your application should turn the string value into a date range and you should use that range in your query: 就是说,我认为您的应用程序应该将字符串值转换为日期范围,并且应该在查询中使用该范围:

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

I strong encourage you to avoid between with dates, particularly in Oracle. 我强烈建议您避免between日期between ,特别是在Oracle中。 The date data type has a built-in time component, and that can throw off the comparisons. date数据类型具有内置的时间组件,可能会导致比较失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM