Re: How to implement GOMONTH function

From: Rodrigo De León <rdeleonp(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to implement GOMONTH function
Date: 2007-05-13 21:26:09
Message-ID: 1179091569.227593.177280@n59g2000hsh.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus ha escrito:
> I need to create function GOMONTH which returns date by given number of
> month before or forward using sql or pgsql in 8.1+
> For example,
> GOMONTH( DATE '20070513', 1 ) should return date '20070613'
> GOMONTH( DATE '20070513', -2 ) should return date '20070313'
>
> I tried
>
> CREATE OR REPLACE FUNCTION public.gomonth(date, integer,
> out date) IMMUTABLE AS
> $_$
> SELECT $1 + $2'months';
> $_$ language sql
>
> but got error
>
> ERROR: syntax error at or near "'months'"
>
> How to implement this ?
>
> Andrus.

CREATE OR REPLACE FUNCTION
PUBLIC.GOMONTH(DATE, INTEGER, OUT DATE) IMMUTABLE AS
$_$
SELECT ($1 + ($2 || 'MONTHS')::INTERVAL)::DATE;
$_$ LANGUAGE SQL

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-05-13 21:35:31 Re: How to implement GOMONTH function
Previous Message Andrus 2007-05-13 20:42:44 How to implement GOMONTH function