Alessandro Nacci wrote:
>
> Hello,
> I'm trying to adding n months to a date.
>
> PowerBuilder provides RelativeDate(date, n) function that allow me to
> add n days to a given date but I don't know how many days I have to add!
>
> SYBASE SQL PROVIDES THIS FUNCTION:
>
> MONTHS( date-expr, integer-expr )
> Add integer-expr months to the given date. If the new date is past
> the end of the month (such as MONTHS('1992-01-31', 1) ) the result is set to
> the last day of the month. If the integer-expr is negative, the appropriate
> number of months are subtracted from the date. Hours, minutes and seconds
> are ignored.
>
> Is there a function like this one in PowerBuilder?? Note that my application
> runs under Sybase, Oracle and MSQL and I don't want to use dynamic cursor to
> execute database functions!! (if is possible)
>
> Thanks in advance and.... sorry for my English
> A.
Here is the function you need.
of_RelativeMonth (date ad_source, long al_month)
date ld_invalid = date (0, 0, 0)
long ll_month, ll_year, ll_day
//Add number of months to the base month
ll_month = month (ad_source) + al_month
//calculate year
ll_year = year (ad_source) + int ((ll_month - 1) / 12)
//calculate month and handle situation when ll_month is negative
ll_month = mod (mod(ll_month, 12) + 11, 12) + 1
//calculate day
ll_day = Day(ad_source)
If ll_day > 28 Then
//handle the situation when day is greater than 28
Do while date (ll_year, ll_month, ll_day) = ld_invalid
ll_day --
Loop
End If
Return date (ll_year, ll_month, ll_day)
--
Lijun
=======================================================
Lijun Yang mailto:Lijun...@bigfoot.com
=======================================================