Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2011

    Unanswered: How to dis play previous month(Dec) details in db2


    Please help me for the following problem in db2.

    In employee table i want to show the previous month salaray details...but i run this query every month 1st.


    select * from employee where year(salary_date)=year(current timestamp) and month(salary_date)=month(current timestamp)-1;

    but here i faced one problem is

    For example
    case 1:
    i want to run this query January month of current year...this time i want to display Last year December ,2010 details.

    case 2:

    if i want to run this curent year after february the above query working fine

    if a run this after feburay the above query get the jan month details.

    pls help for above query.
    Last edited by nsrinusetty; 07-08-11 at 02:20. Reason: i need more information

  2. #2
    Join Date
    Feb 2008
    Apply YEAR and MONTH functions after subtracting 1 month from current date.

    Example: Because you are iquiring salary_date, using current date would be enough.
    select * from employee
     where year (salary_date) = year (current date - 1 month)
       and month(salary_date) = month(current date - 1 month)
    Another example:
    SELECT *
     FROM  employee
     WHERE salary_date
           BETWEEN LAST_DAY(current date - 2 MONTH) + 1 DAY /* first day of previous month */
               AND current date - DAY(current date) DAYs    /*  last day of previous month */
    Last edited by tonkuma; 07-08-11 at 01:54. Reason: Add comments to second example.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts