简体   繁体   English

如何在 oracle 中搜索给定月份和年份的特定日期?

[英]How to search a specific date from a given month and year in oracle?

I have been given a month like 1 for January and a year like 2020. Now I want to search for a date within January-2020.给我的月份是 1 表示一月,一年是 2020。现在我想搜索 2020 年一月之间的日期。 Suppose that day is 14/01/2020, so I'm trying the following code假设那一天是 2020 年 1 月 14 日,所以我正在尝试以下代码

select date 
from table X 
where date between to_date(1||'/'||month||'/'||year,'dd/mm/yyyy') 
               and to_date(31||'/'||month||'/'||year,'dd/mm/yyyy')

but this range is for month January what if the month is February or April which have 30 days?但这个范围是针对一月份的,如果月份是二月或四月,有 30 天怎么办? If this query is not suitable then how to achieve the perfect result?如果这个查询不合适那么如何达到完美的结果呢?

You can do it like below,你可以像下面那样做,

To find first date and last date from a given date,要查找给定日期的第一个日期和最后一个日期,

select trunc(date'2020-02-14','MM') first_day,
       last_day(date'2020-02-14') last_day
  from dual;

Edit: after comments from other users So the final query be like,编辑:在其他用户的评论之后所以最终的查询就像,

select date 
from table X 
where date between trunc(date'2020-02-14','MM') and last_day(date'2020-02-14');

Note: If you store date along with time you might like into the answer provided by @MT0注意:如果您将日期与时间一起存储,您可能会喜欢@MT0 提供的答案

You can transform the dates via to_char to extract the parts you want to compare.您可以通过to_char转换日期以提取要比较的部分。

select * 
from dates
where to_char(the_date, 'mm') = '01'
and to_char(the_date, 'yyyy') = '2020'

SQL-Fiddle for it is here它的 SQL-Fiddle在这里

Use TO_DATE( your_year_value || '-' || you_month_value, 'YYYY-MM' ) to convert the year-month values to a date and then to get the upper bound you can use the ADD_MONTHS function to find the start of the next month:使用TO_DATE( your_year_value || '-' || you_month_value, 'YYYY-MM' )将年月值转换为日期,然后获取上限,您可以使用ADD_MONTHS function 找到下个月的开始:

SELECT *
FROM   table_name
WHERE  date_time >= TO_DATE( 2020 || '-' || 1, 'YYYY-MM' )
AND    date_time <  ADD_MONTHS( TO_DATE( 2020 || '-' || 1, 'YYYY-MM' ), 1 )

(Note: If you use TRUNC or TO_CHAR on your date column then Oracle will not use an index on that column; you would instead need a separate function-based index on the exact expression you used. Instead, by calculating the range on the year/month inputs rather than transforming the date column then Oracle will use indexes on the date column.) (注意:如果您在日期列上使用TRUNCTO_CHAR ,则 Oracle 将不会在该列上使用索引;您需要在您使用的确切表达式上使用单独的基于函数的索引。相反,通过计算年份的范围/month 输入而不是转换日期列然后 Oracle 将使用日期列上的索引。)

(Note 2: You don't want to compare on the range 2020-01-01 to 2020-01-31 as this will ignore any values from 2020-01-31T00:00:01 to 2020-01-31T23:59:59 . Instead, you need to use the range from 2020-01-01 up until, but not including, 2020-02-01 .) (注意 2:您不想在2020-01-012020-01-31的范围内进行比较,因为这将忽略从2020-01-31T00:00:012020-01-31T23:59:59 。相反,您需要使用从2020-01-01到但不包括2020-02-01的范围。)

So, for some test data:所以,对于一些测试数据:

CREATE TABLE table_name ( date_time DATE );

INSERT INTO table_name( date_time )
SELECT DATE '2020-01-14' FROM DUAL UNION ALL
SELECT DATE '2020-01-31' + INTERVAL '1' HOUR FROM DUAL UNION ALL
SELECT DATE '2020-02-01' FROM DUAL

This outputs:这输出:

 | | DATE_TIME |日期_时间 | |:------------------ | |:-------------------- | | | 2020-01-14T00:00:00 | 2020-01-14T00:00:00 | | | 2020-01-31T01:00:00 | 2020-01-31T01:00:00 |

db<>fiddle here db<> 在这里摆弄

You only want to check if month and year match so don't bother about dates.您只想检查月份和年份是否匹配,所以不必担心日期。 Instead of checking if a value is between 2 specific dates you could just check if the month and year match.您可以检查月份和年份是否匹配,而不是检查一个值是否在 2 个特定日期之间。

SELECT date
  FROM table
 WHERE TO_CHAR(date,'MM-YYYY') = '01-2020'

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

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