简体   繁体   中英

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. Suppose that day is 14/01/2020, so I'm trying the following code

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? 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

You can transform the dates via to_char to extract the parts you want to compare.

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

SQL-Fiddle for it is here

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:

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

(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 .)

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-31T01:00:00 | 

db<>fiddle here

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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