From: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-admin@postgresql.org
Subject: Re: format() function with string_agg
Date: Tue, 23 Sep 2014 20:09:04 -0700 (PDT)
Message-ID: <1411528144063-5820251.post@n5.nabble.com> (raw)
In-Reply-To: <CALU_HCOEBFAibCcCSBGKdGL7+Shd5vSZLGVBe6XqmM77SanUJA@mail.gmail.com>
References: <CALU_HCOEBFAibCcCSBGKdGL7+Shd5vSZLGVBe6XqmM77SanUJA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-admin>
Raj Gandhi wrote
> I'm trying to use format() function with string_agg with Postgres 9.1.9
> but
> getting error - "too few arguments for format"
>
> I have two tables t_message and t_message_args. Table t_message has
> 'message' column with text in the format 'Test message first arg is %1$s
> and second arg %2$s end-of-message'
>
> id | integer |
> message | character varying |
>
>
> And the second table t_message_args contains message argument and values
> id | integer |
> arg | integer |
> argvalue | character varying |
>
>
> Here is the SQL that uses format() function to retrieve formatted message
> by replacing arg value in the message:
> =
> select m.id, format(m.message, string_agg(a.argvalue, ',' order by a.arg)
> from t_message m, t_message_args a
> where m.id = a.id
> group by m.id, m.message
>
>
> ERROR: too few arguments for format
>
> ********** Error **********
>
> ERROR: too few arguments for format
> SQL state: 22023
format('%s %s', '1', '2'); --works
format('%s %s, array['1','2']) -- doesn't work
http://www.postgresql.org/docs/9.3/interactive/xfunc-sql.html
(arguably this is not SQL language specific and should be relocated to a
more generic part of the documentation...)
> You can't actually write that, though — or at least, it will not match
> this function definition. A parameter marked VARIADIC matches one or more
> occurrences of its element type, not of its own type.
>
> Sometimes it is useful to be able to pass an already-constructed array to
> a variadic function; this is particularly handy when one variadic function
> wants to pass on its array parameter to another one. You can do that by
> specifying VARIADIC in the call:
select format('%s %s', VARIADIC array_agg(val)) from (values ('1'),('2'))
src (val) -- works
Note the use of array_agg(); string_agg() returns text, not an array, so its
not going to be of use here - unless you want to build up the specifier
portion on the fly too:
select format(string_agg(fmt_prt, ';'), array_agg(val))
from (values ('%s','1'),('%s','2')) src (fmt_prt, val) --works
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/format-function-with-string-agg-tp5820247p5820251.html
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.
--
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: format() function with string_agg
In-Reply-To: <1411528144063-5820251.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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