pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedplpgsql related question: intervals and variables
4+ messages / 4 participants
[nested] [flat]
* plpgsql related question: intervals and variables
@ 2003-10-21 13:58 Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>
0 siblings, 2 replies; 4+ messages in thread
From: Wilhelm Graiss @ 2003-10-21 13:58 UTC (permalink / raw)
To: pgsql-sql
Hello out there,
We have a problem in plpgsql:
We want to add variable periods (result of a query) to a timestamp.
Our Code looks like this:
Begin
heute := ''today'';
Select Into vk ourcolumn From table where other = foo;
If vk > 0 Then
vk_txt := ''Vorkuehlung notwendig'';
ez := heute + interval ''vk days'';
Else
vk_txt := ''Keine Vorkuehlung vorgeschrieben'';
End if;
We get the following:
ERROR: Bad interval external representation 'vk days'
The variable 'heute' is declared as timestamp,
'vk' as integer!
What have we done wrong??
:(
Thanks in advance,
Willi, Albin
--
=============================
Wilhelm Graiss
Altirdning 12
8952 Irdning
03682/22451/267
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: plpgsql related question: intervals and variables
@ 2003-10-21 18:43 Josh Berkus <josh@agliodbs.com>
parent: Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>
1 sibling, 1 reply; 4+ messages in thread
From: Josh Berkus @ 2003-10-21 18:43 UTC (permalink / raw)
To: Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>; pgsql-sql
Willhelm,
> Begin
>
> heute := ''today'';
> Select Into vk ourcolumn From table where other = foo;
> If vk > 0 Then
> vk_txt := ''Vorkuehlung notwendig'';
> ez := heute + interval ''vk days'';
PL/pgSQL handles variable like SQL, not like PHP or Perl. You can't do a
variable substitution inside quotes, and you need to cast:
ez := heute + interval (cast(vk as text) || '' days'');
Also, the string 'today' has no special meaning in PL/pgSQL. I think you want
now() instead.
I'm afraid that you're going to need a tutorial on SQL datatypes, casting, and
similar issues ... I wish I had one to recommend to you. Just keep in mind
that SQL scripting languages (like PL/pgSQL) are not Perl!
--
-Josh Berkus
Aglio Database Solutions
San Francisco
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: plpgsql related question: intervals and variables
@ 2003-10-21 18:54 Richard Huxton <dev@archonet.com>
parent: Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>
1 sibling, 0 replies; 4+ messages in thread
From: Richard Huxton @ 2003-10-21 18:54 UTC (permalink / raw)
To: Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>; pgsql-sql
On Tuesday 21 October 2003 14:58, Wilhelm Graiss wrote:
> heute := ''today'';
> Select Into vk ourcolumn From table where other = foo;
> If vk > 0 Then
> vk_txt := ''Vorkuehlung notwendig'';
> ez := heute + interval ''vk days'';
> The variable 'heute' is declared as timestamp,
> 'vk' as integer!
>
> What have we done wrong??
Quoted the vk variable. You want something like:
ez := heute + (vk || '' days'')::interval;
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: plpgsql related question: intervals and variables
@ 2003-10-21 21:39 Stephan Szabo <sszabo@megazone.bigpanda.com>
parent: Josh Berkus <josh@agliodbs.com>
0 siblings, 0 replies; 4+ messages in thread
From: Stephan Szabo @ 2003-10-21 21:39 UTC (permalink / raw)
To: Josh Berkus <josh@agliodbs.com>; +Cc: Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>; pgsql-sql
On Tue, 21 Oct 2003, Josh Berkus wrote:
> > heute := ''today'';
> > Select Into vk ourcolumn From table where other = foo;
> > If vk > 0 Then
> > vk_txt := ''Vorkuehlung notwendig'';
> > ez := heute + interval ''vk days'';
>
> PL/pgSQL handles variable like SQL, not like PHP or Perl. You can't do a
> variable substitution inside quotes, and you need to cast:
>
> ez := heute + interval (cast(vk as text) || '' days'');
I think that something like
ez := heute + vk * interval '1 day';
might be better in general.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2003-10-21 21:39 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-10-21 13:58 plpgsql related question: intervals and variables Wilhelm Graiss <Wilhelm.Graiss@bal.bmlfuw.gv.at>
2003-10-21 18:43 ` Josh Berkus <josh@agliodbs.com>
2003-10-21 21:39 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2003-10-21 18:54 ` Richard Huxton <dev@archonet.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox