Avatar of gmollineau
gmollineau
Flag for Trinidad and Tobago asked on

Select from Oracle DB all rows going back to 1-Jan four years ago.

Hi


I want to Select from Oracle DB all rows where YEAR portion of invoice = Current Year - 4 years.

I always want the selection to start from January 1 four years ago. 


Therefore if the current date is 15-April 2022. I want the selection to start at 1-Jan-2018


I used the following but this starts at 15-Apr- 2018


and to_date(INVOICE_DATE) between  to_date(SYSDATE) -4*365.25 and to_date(SYSDATE)


Thanks 


Gerald

* Oracle PL/SQLOracle Database

Avatar of undefined
Last Comment
Sean Stuber

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sean Stuber

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

To always start on Jan 1, something like this?

SELECT ...
WHERE invoice_date >= to_date('01-Jan-' || to_char (to_number(sysdate, 'YY')-4))

Sean Stuber

to_number(sysdate, 'YY')

That's not reliable syntax

If you did want to do extra type conversions then something like this would be more reliable

to_number(to_char(sysdate,'yyyy'),'9999')

But, I think just doing direct date math is more efficient.
gmollineau

ASKER
suggested exactly what i needed
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Kent Olsen

Agreed, but I don't think that the first SQL answer the question.

  ... invoice_date between add_months(trunc(sysdate,'yyyy'),-48) and sysdate

Run today, that would window invoices from Feb 14, 2018 and today.  (Month - 1) and (Day - 1) need to be subtracted to get to Jan 1.


gmollineau

ASKER
Actually I tested it on 2 Oracle DB already.

both go back to 1-Jan 2018.
SOLUTION
Sean Stuber

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gmollineau

ASKER
Thank again. 
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

I had no idea that trunc(x,'YYYY') returned Jan 1 and I've been in this business a looonnnnnnnnnnggg time!

Thank for cluing me in!

Sean Stuber

You're wlcome!

You can trunc to a lot of different resolutions...

It's an incredibly helpful function.

I wrote an article about it last year exploring some of its utility

https://seanstuber.com/2021/01/30/dates-and-timestamps-the-trunc-function/