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?

Thanks,

Karen
Karen SchaeferBI ANALYSTAsked:
Who is Participating?
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)
0
slightwv (䄆 Netminder) Commented:
select trunc(add_months(sysdate,-12),'YEAR') from dual;

returns: 01/01/2017 00:00:00
0
Karen SchaeferBI ANALYSTAuthor Commented:
do these return ytd to current date?  neither of these work -
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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.
0
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.
0
Karen SchaeferBI ANALYSTAuthor Commented:
Got the following error:

ORA-00932: inconsistent datatypes: expected NUMBER got DATE
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:
Error at Line: 13 Column: 90
0
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;
0
Karen SchaeferBI ANALYSTAuthor Commented:
still getting the error message.
0
slightwv (䄆 Netminder) Commented:
What is the actual data type of the column posted_on_dt_wid?
0
Karen SchaeferBI ANALYSTAuthor Commented:
POSTED_ON_DT_WID      NUMBER(10,0)      No
0
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.
0
Karen SchaeferBI ANALYSTAuthor Commented:
DATA SAMPLE

POSTED_ON_DT_WID
20160407
20160407
20160407
20160407
20160407
0
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

0

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.