| |
|
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.
|
 |

04-22-09, 02:29
|
|
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
|
|

04-22-09, 03:14
|
|
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
|
|

04-22-09, 03:19
|
|
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
|
|

04-22-09, 03:33
|
|
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
|
|

04-22-09, 04:33
|
|
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.
|

04-22-09, 04:44
|
|
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
|
|

04-22-09, 05:07
|
|
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.
|
|

04-22-09, 05:26
|
|
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.
|
|

04-22-09, 05:39
|
|
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
|
|

04-22-09, 05:46
|
|
Registered User
|
|
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
|
|
|
|

04-22-09, 05:56
|
|
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.
|
|

04-22-09, 13:52
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|