Skip to content
Advertisement

How to find all records that are older than or equal to the current month of the previous year?

I barely know SQL. I am trying to achieve the following:

For example I have the following table that has 2 fields

I need to find all whose birthdays are current month of the previous year and prior. For example if I am running the query on 08/15/2021, I need to find all records that have a month/date of 08/2020 (ignoring the day part) and prior. So some entries may be less than 12 months old (those after the day of query in the current month of prior year) and would get need to get included in the returned rows.

if I have the following records and I am running the query on 8/15/2021

It needs to return the following:

If I did something like the following,

it would exclude the entries for the current month of previous year that are after the date when the query is run. I can get the desired result using convoluted queries but I was wondering if there was an easier way of doing this, e.g. an oracle function.

Advertisement

Answer

You can compare with the first day of next month, ie 11 months ago instead of 12:

Full example with test data:

DBFIDDLE: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=b9f4889a47f59f6a48d3fb2d0a212617

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement