Oracle date question

Posted on 2014-01-31
Medium Priority
Last Modified: 2014-02-04

   select nvl(sum(recovered_shp_liters_qty), 0)recovered_shp_qty
     from pla_quantities
    where blood_center_key = 2
      and process_date between :p_start_date and :coll_date;

I am doing a graph. For the above period I am getting the graph. Now I need to compare the graph for the data for 2012. No matter what date range they enter I have to go back to 2012.

I can hardcode p_start_date to be '01-jan-2012'
but coll_date is the one I get from the table to do year to date graph. My question is based on what the coll_date is can we get that to go back to 2012 ?
Can I use a decode or case statement to go back to 2012 of coll_date?

  and coll_date has to be
Question by:anumoses
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 35

Accepted Solution

johnsone earned 1000 total points
ID: 39824135
Assuming that coll_date could be any year, then this should give the same date in 2012

to_date(to_char(coll_date, 'mmddhh24miss')||'2012', 'mmddhh24missyyyy')

Author Comment

ID: 39824157
If I dont need the time stamp then will this be corect?

LVL 35

Expert Comment

ID: 39824176
Yes.  coll_date has a time portion to it (all dates do), so I was just copying what was there.  If the time portion is 00:00:00 in the original, that that will just get copied.  No harm.
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

LVL 32

Expert Comment

ID: 39824915
Another way -
to_date('2012','yyyy') + (coll_date - trunc(coll_date,'yy'))
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39825000
>>Another way -

Seems to drop a day from time to time (unless I have a typo):
select to_date('2012','yyyy') + (to_date('10/10/2013') - trunc(to_date('10/10/2013'),'yy')) from dual;
LVL 32

Expert Comment

ID: 39825047
Yeah, the problem here is that 2012 is a leap year. The difference in days of a non-leap year date and the first of that year will not produce the same day for a leap year unless the date is on or before February 28. One could create a case statement to deal with that, of course, but that might perform worse that converting to character and back to date again. :-(
LVL 35

Expert Comment

ID: 39825149
Well, if you want to get fancy, then you could do this:

add_months(coll_date, (2012 - to_number(to_char(coll_date, 'yyyy')))*12)

Still has some conversions to it though.

It would be best to stay away from using the "day" math.  Almost impossible to account for leap years, as you have seen.

Author Closing Comment

ID: 39832315

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question