If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to dis play previous month(Dec) details in db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-07-11, 23:54
nsrinusetty nsrinusetty is offline
Registered User
 
Join Date: Jul 2011
Posts: 4
How to dis play previous month(Dec) details in db2

Hi,

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.

Query:

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
Reply With Quote
  #2 (permalink)  
Old 07-08-11, 01:38
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
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.
Code:
select * from employee
 where year (salary_date) = year (current date - 1 month)
   and month(salary_date) = month(current date - 1 month)
Another example:
Code:
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On