Home > Mobile >  Exclude Certain Year from a Date Column
Exclude Certain Year from a Date Column

Time:02-10

I'm using Oracle Database version 12.1.0.2.0. I'm running a query that returns two columns, "Permit #" and "Date Issued."

SELECT id.B1_ALT_ID AS "Permit #", process.REC_DATE AS "Date Issued"
FROM process
LEFT JOIN id ON ( id.B1_PER_ID1 = process.B1_PER_ID1 AND id.B1_PER_ID3 = process.B1_PER_ID3 )
WHERE id.B1_PER_ID1 like 'B2021%'
AND process.SD_APP_DES = 'Issued';

Sample result:

enter image description here

Question: How can I exclude records from the year 2022?

I tried adding to my original query the following clause to exclude the year:

AND TO_CHAR( process.REC_DATE ) NOT LIKE ' 22%';

But I received the same result. How can I results that don't include 2022 in the "Date Issued" column?

CodePudding user response:

WHERE process.rec_date <  DATE '2022-01-01'
OR    process.rec_date >= DATE '2023-01-01'

(Note: this may use an index on the date_issued column. The options below would require a function-based index to use an index.)

or

WHERE EXTRACT(YEAR FROM process.rec_date) != 2022

or

WHERE TO_CHAR(process.rec_date, 'YYYY') != '2022'

or

WHERE TRUNC(process.rec_date, 'YYYY') != DATE '2022-01-01'
  • Related