Re: subtracting from a date

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: subtracting from a date
Date: 2014-02-06 22:07:43
Message-ID: 1391724463623-5790923.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

jvsrvcs wrote
> I tried that but get an error
> v_start_date date;
> v_minus_one_year date;
>
> I have v_start_date to start with and want to subtract one year and put
> into v_minus_one_year
>
> select v_start_date - interval '1 yr' as v_minus_one_year;
>
>
>
> --- I get:
>
> ERROR: query has no destination for result data

The relevant section of the documentation you need to study is:

http://www.postgresql.org/docs/9.3/interactive/plpgsql-statements.html

There are two basic ways to assign to variables:

variable := expression
SELECT expression[s] INTO variable[s] FROM ... || SELECT expression[s] FROM
... INTO variable[s]

Your problem is that:

SELECT expression AS "variable" FROM ... simply provides an alias for the
expression and has nothing to do with any variables in the surrounding
program. The "INTO" keyword is needed to avoid ambiguity.

pl/pgsql has the unique behavior that using SELECT without INTO results in
an error. If you really need to execute a SELECT and ignore the content
selected you have to use PERFORM. The error you saw was this behavior in
action.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/subtracting-from-a-date-tp5790891p5790923.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andy Colson 2014-02-06 22:30:49 Re: Offending My Tender Sensibilities -OR- OLTP on a Star Schema
Previous Message wd 2014-02-06 20:24:08 Exclude pg_log directory when running pg_basebackup?