Thread: Re: DateAdd function ?

Re: DateAdd function ?

From
"Jeff Eckermann"
Date:
"Zlatko Mati�" <zlatko.matic1@sb.t-com.hr> wrote in message 
news:d42nl1$7al$1@ss405.t-com.hr...
>I am currently migrating from MSDE to PostgreSQL and have to rewrite the 
>function that is calculating next date of sampling...
> In MSDE there is a DateAdd function. I can't find the appropriate function 
> in postgre. Can you help me?

There is no "dateadd" function in PostgreSQL, but you can write your own 
easily enough.  The following should give you an idea of the logic you can 
use:

jeck=# select current_date;   date
------------2005-04-19
(1 row)

jeck=# select current_date + cast('1 day' as interval);       ?column?
------------------------2005-04-20 00:00:00-05
(1 row)

jeck=# select current_date + cast('3 months' as interval);       ?column?
------------------------2005-07-19 00:00:00-05
(1 row)

jeck=# select current_date + 3 * cast('1 month' as interval);       ?column?
------------------------2005-07-19 00:00:00-05
(1 row)


> The function in MSDE is the following:
>
> ALTER FUNCTION dbo.slisp
> (
> @UCESTALOST_BROJ int,
> @UCESTALOST_JEDINICA nvarchar (50),
> @DATUM_ISPITIVANJA datetime
> )
> RETURNS datetime
> AS
> BEGIN
>  DECLARE @SLISP datetime
>   IF @UCESTALOST_JEDINICA='m' SET @SLISP=DATEADD(month, @UCESTALOST_BROJ, 
> MAX(@DATUM_ISPITIVANJA))
>   IF @UCESTALOST_JEDINICA='g' SET @SLISP=DATEADD(year, @UCESTALOST_BROJ, 
> MAX(@DATUM_ISPITIVANJA))
>   IF @UCESTALOST_JEDINICA='d' SET @SLISP=DATEADD(day, @UCESTALOST_BROJ, 
> MAX(@DATUM_ISPITIVANJA))
>  RETURN @SLISP
> END
>
> Thanks.
> 




Re: DateAdd function ?

From
Tom Lane
Date:
"Jeff Eckermann" <jeff_eckermann@yahoo.com> writes:
> There is no "dateadd" function in PostgreSQL, but you can write your own 
> easily enough.  The following should give you an idea of the logic you can 
> use:

> jeck=# select current_date + cast('1 day' as interval);

Alternatively, maybe you want to use the date-plus/minus-integer operators.

regression=# select current_date;   date
------------2005-04-19
(1 row)

regression=# select current_date + 1; ?column?
------------2005-04-20
(1 row)

regression=# select current_date - 1; ?column?
------------2005-04-18
(1 row)

There isn't really a date-plus-interval operator --- what's happening
there is the date is implicitly promoted to timestamp and then
timestamp-plus-interval is used.  You might want this if you want to
add symbolic quantities like '2 months' or '1 year', but for small
integer numbers of days, the integer operations are faster and simpler
to use.
        regards, tom lane