Re: DateAdd function ? - Mailing list pgsql-sql

From Jeff Eckermann
Subject Re: DateAdd function ?
Date
Msg-id d4348v$30ii$2@news.hub.org
Whole thread Raw
Responses Re: DateAdd function ?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
"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.
> 




pgsql-sql by date:

Previous
From: "Jeff Eckermann"
Date:
Subject: Re: Query about SQL in PostgreSQL
Next
From: "Jeff Eckermann"
Date:
Subject: Re: Query about SQL in PostgreSQL