Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XWcx8-0006cB-Q0 for pgsql-admin@arkaria.postgresql.org; Wed, 24 Sep 2014 03:09:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XWcx7-0002BI-RG for pgsql-admin@arkaria.postgresql.org; Wed, 24 Sep 2014 03:09:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XWcx6-0002BB-OT for pgsql-admin@postgresql.org; Wed, 24 Sep 2014 03:09:12 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XWcx1-0002sZ-ID for pgsql-admin@postgresql.org; Wed, 24 Sep 2014 03:09:10 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1XWcwy-000466-2h for pgsql-admin@postgresql.org; Tue, 23 Sep 2014 20:09:04 -0700 Date: Tue, 23 Sep 2014 20:09:04 -0700 (PDT) From: David G Johnston To: pgsql-admin@postgresql.org Message-ID: <1411528144063-5820251.post@n5.nabble.com> In-Reply-To: References: Subject: Re: format() function with string_agg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org 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" >=20 > 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' >=20 > id | integer | > message | character varying | >=20 >=20 > And the second table t_message_args contains message argument and values > id | integer | > arg | integer | > argvalue | character varying | >=20 >=20 > Here is the SQL that uses format() function to retrieve formatted message > by replacing arg value in the message: > =3D > 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 =3D a.id > group by m.id, m.message >=20 >=20 > ERROR: too few arguments for format >=20 > ********** Error ********** >=20 > 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 =E2=80=94 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. >=20 > 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))=20 from (values ('%s','1'),('%s','2')) src (fmt_prt, val) --works David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/forma= t-function-with-string-agg-tp5820247p5820251.html Sent from the PostgreSQL - admin mailing list archive at Nabble.com. --=20 Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin