Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

YTD report dates in oracle

Posted on 2013-01-08
11
Medium Priority
?
635 Views
Last Modified: 2013-01-08
select TRUNC(TO_DATE(sysdate),'YEAR'),
       (last_day(trunc(add_months(sysdate,-1)))) + 1 - (1/86400)
  from dual

TRUNC(TO_DATE(SYSDATE)-1,'YEAR')|(LAST_DAY(TRUNC(ADD_MONTHS(SYSDATE,-1))))+1-(1/86400)
1/1/2013|12/31/2012 11:59:59 PM

_____________________

I need to run a year to date report.
In January 2013 I need the date parameters from 1/1/2012 to 12/31/2012
In feb 2013 I need the date parameters from 1/1/2013 to 1/31/2013

In december 2013 from 1/1/2013 to 11/30/2012
In Jan 2014 from 1/1/2013 to 12/31/2013.

Any help appreciated
0
Comment
Question by:anumoses
[X]
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
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 38756519
for results to the last day...

SELECT TRUNC(ADD_MONTHS(SYSDATE, -1), 'yyyy'), TRUNC(SYSDATE, 'mm') - 1 FROM DUAL


for results to the last second...

SELECT TRUNC(ADD_MONTHS(SYSDATE, -1), 'yyyy'), TRUNC(SYSDATE, 'mm') - 1/86400 FROM DUAL
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 1000 total points
ID: 38756529
simple verification code...


SELECT d, TRUNC(ADD_MONTHS(d, -1), 'yyyy'), TRUNC(d, 'mm') - 1 / 86400
  FROM (SELECT     ADD_MONTHS(TO_DATE('20120615', 'yyyymmdd'), LEVEL) d
              FROM DUAL
        CONNECT BY LEVEL <= 24)
0
 
LVL 6

Author Comment

by:anumoses
ID: 38756534
On feb 2nd if I want to run from 01-jan-2013 to 31-jan-2013, will the same query work?

Thanks,
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 23

Expert Comment

by:Steve Wales
ID: 38756539
Post here that talks about finding dates based upon current / previous / next value of year, month etc.
http://programmerslounge.blogspot.com/2011/07/how-to-get-first-day-and-last-day-of.html

You seem to always need the first day of the year represented by the previous month
If you're in January, you want the 1st day of the query to be January 1 of the previous year:

select TRUNC(ADD_MONTHS(SYSDATE, -1) , 'Year') from dual;

That would give you midnight of the first day of the year.

To get the last day of the previous month at 23:59:59 you could take the first day of the current month -1 second:

select TRUNC(SYSDATE , 'Month')-1/86400 from dual;

Running those two queries today (Jan 8, 2013) I get

2012-01-01 00:00:00
2012-12-31 23:59:59
0
 
LVL 6

Author Comment

by:anumoses
ID: 38756541
Oh that helped a lot. Thanks,
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38756560
>>> on feb 2nd if I want to run from 01-jan-2013 to 31-jan-2013, will the same query work?


yes, adjust the verification query to test whatever dates you want


it will correctly handle leap years too
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 38756605
thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 38756697
select * from yourtable where datefield between
decode(to_char(sysdate,'mm'),'01',trunc(add_months(sysdate,-1),'yyyy'),trunc(sysdate,'yyyy')) and trunc(sysdate,'mm') -1/86400
from dttbl;
0
 
LVL 32

Expert Comment

by:awking00
ID: 38756704
Didn't mean to copy "from dttbl;" (my test table)

select * from yourtable where datefield between
decode(to_char(sysdate,'mm'),'01',trunc(add_months(sysdate,-1),'yyyy'),trunc(sysdate,'yyyy')) and trunc(sysdate,'mm') -1/86400;
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38756712
why the decode?  

trunc(add_months(sysdate,-1),'yyyy')


from my original post is sufficient for all months
try the verification code in my second post to confirm
0
 
LVL 32

Expert Comment

by:awking00
ID: 38756735
I was a little late noticing it wasn't necessary and didn't bother changing since your solution had already been accepted by the time I posted :-)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

704 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