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 > next month end date based on input date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-22-09, 02:29
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
next month end date based on input date

how to find next month end date based on input date.

inpute date= 2009-01-31

next month end date will be 2009-02-28
Reply With Quote
  #2 (permalink)  
Old 04-22-09, 03:14
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
values date('2009-01-31')- day('2009-01-31') days + 2 months
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 04-22-09, 03:19
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
ignore last soln wont work always '2009-03-31'
I think you will have to use case stmt and handle feb and leap year it that
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #4 (permalink)  
Old 04-22-09, 03:33
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
similarly for April month end date

it worked for feb month end and March but not for April
Reply With Quote
  #5 (permalink)  
Old 04-22-09, 04:33
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Try this:

SELECT ADD_MONTHS(LAST_DAY(your-date),1)
FROM SYSIBM.SYSDUMMY1



for example:
SELECT ADD_MONTHS(LAST_DAY(DATE('31.01.2009')),1)
FROM SYSIBM.SYSDUMMY1

Last edited by umayer; 04-22-09 at 04:36.
Reply With Quote
  #6 (permalink)  
Old 04-22-09, 04:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Don't use a CASE expression - that's not necessary. You only forgot to substract another day:
Code:
$ db2 "values date('2009-01-31') - day('2009-01-31') days + 2 months - 1 day"

1
----------
02/27/2009

  1 record(s) selected.

$ db2 "values date('2008-01-31') - day('2008-01-31') days + 2 months - 1 day"

1
----------
02/28/2008

  1 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 04-22-09, 05:07
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Code:
------------------------------ Commands Entered ------------------------------
SELECT in_date
     , in_date - (DAY(in_date) - 1) DAYS + 2 MONTHs - 1 DAY  AS end_of_next_month
  FROM (VALUES date('2008-01-31')
             , date('2009-01-31')
             , date('2009-02-01')
             , date('2009-02-28')
             , date('2009-03-01')
             , date('2009-03-31')
             , date('2009-04-01')
             , date('2009-04-30') ) t(in_date)
;
------------------------------------------------------------------------------

IN_DATE    END_OF_NEXT_MONTH
---------- -----------------
2008-01-31 2008-02-29       
2009-01-31 2009-02-28       
2009-02-01 2009-03-31       
2009-02-28 2009-03-31       
2009-03-01 2009-04-30       
2009-03-31 2009-04-30       
2009-04-01 2009-05-31       
2009-04-30 2009-05-31       

  8 record(s) selected.
Reply With Quote
  #8 (permalink)  
Old 04-22-09, 05:26
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Looking into Manuals...
ADD_MONTHS and LAST_DAY are supported by DB2 for z/OS V9 and DB2 for iSeries V6R1.
But, they are not supported by DB2 for LUW V9.5.
Reply With Quote
  #9 (permalink)  
Old 04-22-09, 05:39
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by tonkuma
Looking into Manuals...
ADD_MONTHS and LAST_DAY are supported by DB2 for z/OS V9 and DB2 for iSeries V6R1.
But, they are not supported by DB2 for LUW V9.5.
... and the above construct has the problem that ADD_MONTHS() is applied after LAST_DAY() so that you will get wrong results if the current month has less days than the target month.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 04-22-09, 05:46
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
This DB2 Basics: Fun with Dates and Times should be part of your bookmarks!
Reply With Quote
  #11 (permalink)  
Old 04-22-09, 05:56
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
ADD_MONTHS adjust day part of result date differently from "+ n MONTHS" labeled expression.
If day part of current date is last day of the month and the current month has less days than the target month, it will return last day of target month.
Reply With Quote
  #12 (permalink)  
Old 04-22-09, 13:52
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
That's really strange in my opinion. I guess this special behavior doesn't apply to the next-to-last day in the month? I would avoid such constructs.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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