We help IT Professionals succeed at work.

Sysdate and Current Month

Larry Brister
on
Medium Priority
5,743 Views
Last Modified: 2008-02-26
Hello,
  I have an Oracle statement that returns product information for the past 120 days.

I need to modift the date part so that I get a current "Month to date" data set.
You can see my sysdate part below.  How do I modify this to get only the current month?

SELECT   invc.acct_dt, part.user_part_no, Sum(inv.qty) as QTY
    FROM invc invc, invc_ln_prod_fam inv, part part
   WHERE (    (invc.invc_no = inv.invc_no)
          AND (inv.part_no = part.part_no)
          AND (   (invc.invc_status_cd = 'OPEN')
               OR (invc.invc_status_cd = 'CLOSED')
              )
          AND (invc.invc_type_cd = 'INV')
              and trunc(invc.acct_dt) > trunc(sysdate -120)
         )
GROUP BY invc.acct_dt, part.user_part_no
ORDER BY part.user_part_no ASC
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi lrbrister,

to_number(to_char(SYSDATE, 'yyyymm'))

I thibk it is better to include year too because ortherwise feb 2006 will be less than dec 2005

HTH

I
Larry Bristersr. Developer

Author

Commented:
ivostoykov,
  I am a SQLServer guy.  Could you please place that code in context with the code below?

and trunc(invc.acct_dt) > trunc(sysdate -120)
CERTIFIED EXPERT

Commented:
Hi lrbrister,

> and trunc(invc.acct_dt) > trunc(sysdate -120)
and to_number(to_char(trunc(invc.acct_dt, 'yyyymm'))) > to_number(to_char(trunc(sysdate -120, 'yyyymm')))

I'm not sure whether you need trunc - it rounds date to 16th date - but it is up to you... take a look

anyway here is without trunc
and to_number(to_char(invc.acct_dt, 'yyyymm')) > to_number(to_char(sysdate -120, 'yyyymm'))

regards

I
Larry Bristersr. Developer

Author

Commented:
ivostoykov,
  I appreciate your quick response.  Maybe I'm not being clear...my fault.

I just want data returned for the current month.  I don't want the "120" calculation returned or used at all.

So whatever the day it is this month...I just get March 2006 data and March 2006 only.

Same applies once the month or April comes.  Whether it's the 1st or the 30th I get the sum total of April data....once Sysdate is within April.
CERTIFIED EXPERT
Commented:
Hi lrbrister,

> h
then you need
and to_number(to_char(invc.acct_dt, 'yyyymm')) = to_number(to_char(sysdate, 'yyyymm'))

for March 2006 data and March 2006 the result will be
200603 (your field) = 200603 (today) and will return true
if your field is from las year then it will looks like
200503 (your field) = 200603 (today) and will return false

hope it is clear...


regards

I

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Larry Bristersr. Developer

Author

Commented:
ivostoykov,
  That's the ticket!  Great job.  Thanks and points being awarded now.;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.