Re: DateAdd function ?

From: "Jeff Eckermann" <jeff_eckermann(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: DateAdd function ?
Date: 2005-04-19 14:23:30
Message-ID: d4348v$30ii$2@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


"Zlatko Mati" <zlatko(dot)matic1(at)sb(dot)t-com(dot)hr> wrote in message
news:d42nl1$7al$1(at)ss405(dot)t-com(dot)hr(dot)(dot)(dot)
>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.
>

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jeff Eckermann 2005-04-19 14:25:43 Re: Query about SQL in PostgreSQL
Previous Message Jeff Eckermann 2005-04-19 14:17:57 Re: Query about SQL in PostgreSQL