Oracle Previous Year to current date.

Help with oracle function to determine the previous year and YTD data values.  I have research the web but not finding what I am looking for.

I need to see 2017  thru current date here is the field name.  "f.posted_on_dt_wid"

What is the proper syntax?


Karen SchaeferBI ANALYSTAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

johnsoneSenior Oracle DBACommented:
f.posted_on_dt_wid >= add_months(trunc(sysdate, 'YY'), -12) and
f.posted_on_dt_wid <= add_months(sysdate, -12)
slightwv (䄆 Netminder) Commented:
select trunc(add_months(sysdate,-12),'YEAR') from dual;

returns: 01/01/2017 00:00:00
Karen SchaeferBI ANALYSTAuthor Commented:
do these return ytd to current date?  neither of these work -
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

slightwv (䄆 Netminder) Commented:
sysdate returns current date down to the exact second of execution.

So, try:
f.posted_on_dt_wid >= trunc(add_months(sysdate,-12),'YEAR') and f.posted_on_dt_wid < trunc(sysdate)+1

I used trunc(sysdate)+1 to mean tomorrow at 00:00:00.

If you want something different, please provide more detail about the exact requirement.

Sample data and expected results help a lot.
Karen SchaeferBI ANALYSTAuthor Commented:
with startdate AS
(select trunc(add_months(sysdate,-12),'YEAR') from dual)
with end_date AS
  (Selelct TO_DATE(sysdate) from dual)

Please help with End_Date - should be current date.
Karen SchaeferBI ANALYSTAuthor Commented:
Got the following error:

ORA-00932: inconsistent datatypes: expected NUMBER got DATE
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
Error at Line: 13 Column: 90
slightwv (䄆 Netminder) Commented:
forget CTE.  sysdate is already a date so no need to use TO_DATE.

Just put things in the where clause:

select col1,col2,col2 from some_table f
where f.posted_on_dt_wid >= trunc(add_months(sysdate,-12),'YEAR') and f.posted_on_dt_wid < trunc(sysdate)+1;
Karen SchaeferBI ANALYSTAuthor Commented:
still getting the error message.
slightwv (䄆 Netminder) Commented:
What is the actual data type of the column posted_on_dt_wid?
Karen SchaeferBI ANALYSTAuthor Commented:
POSTED_ON_DT_WID      NUMBER(10,0)      No
slightwv (䄆 Netminder) Commented:
That is what is causing the error.

We cannot know what you don't tell us.  You are wanting to pull back rows based on dates but have a number column.

We need to know what that column stores and how the date values are interpreted.

Then you can take the date math we provided and convert it into numbers.

Show us a value for POSTED_ON_DT_WID and what date/time value it represents.
Karen SchaeferBI ANALYSTAuthor Commented:

slightwv (䄆 Netminder) Commented:
Assuming that is YYYYMMDD:
select col1,col2,col2 from some_table f
where f.posted_on_dt_wid >= to_number(to_char(trunc(add_months(sysdate,-12),'YEAR'),'YYYYMMDD')) and 
    f.posted_on_dt_wid < to_number(to_char(trunc(sysdate)+1,'YYYYMMDD'));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks that did the trick
Mark GeerlingsDatabase AdministratorCommented:
If anyone has managed to put any invalid values into this column (things like "20160230" or "20161301") or any incomplete values (less than eight digits) the query from slightwv will also fail.  If that happens you will need to clean up or exclude the "bad" rows.  Let us know if you need help with that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.