Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 411C11975914 for ; Wed, 20 Jun 2012 15:22:02 -0300 (ADT) Received: from mh8.mail.rice.edu ([128.42.201.24]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1ShPXR-00050i-MH for pgsql-sql@postgresql.org; Wed, 20 Jun 2012 18:22:00 +0000 Received: from mh8.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh8.mail.rice.edu (Postfix) with ESMTP id 6DDAC291BC3; Wed, 20 Jun 2012 13:21:43 -0500 (CDT) Received: from mh8.mail.rice.edu (localhost.localdomain [127.0.0.1]) by mh8.mail.rice.edu (Postfix) with ESMTP id 5142B297624; Wed, 20 Jun 2012 13:21:43 -0500 (CDT) X-Virus-Scanned: by amavis-2.6.4 at mh8.mail.rice.edu, auth channel Received: from mh8.mail.rice.edu ([127.0.0.1]) by mh8.mail.rice.edu (mh8.mail.rice.edu [127.0.0.1]) (amavis, port 10026) with ESMTP id 9Xv7WdNYDN3m; Wed, 20 Jun 2012 13:21:43 -0500 (CDT) X-SMTP-Auth: no X-SMTP-Auth: no X-SMTP-Auth: no Received: from aart.rice.edu (aart.rice.edu [168.7.56.48]) by mh8.mail.rice.edu (Postfix) with ESMTP id 1BAAE291BC3; Wed, 20 Jun 2012 13:21:43 -0500 (CDT) Received: by aart.rice.edu (Postfix, from userid 18612) id 14795100268; Wed, 20 Jun 2012 13:21:43 -0500 (CDT) Date: Wed, 20 Jun 2012 13:21:43 -0500 From: "ktm@rice.edu" To: Wes James Cc: emilu@encs.concordia.ca, pgsql-sql@postgresql.org Subject: Re: Simple method to format a string Message-ID: <20120620182142.GF6547@aart.rice.edu> References: <4FE1E14A.8060603@encs.concordia.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-12-10) X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201206/61 X-Sequence-Number: 36715 On Wed, Jun 20, 2012 at 12:08:24PM -0600, Wes James wrote: > On Wed, Jun 20, 2012 at 8:42 AM, Emi Lu wrote: > > > Good morning, > > > > Is there a simply method in psql to format a string? > > > > For example, adding a space to every three consecutive letters: > > > > abcdefgh -> *** *** *** > > > > Thanks a lot! > > Emi > > > > > I looked at "format" here: > > http://www.postgresql.org/docs/9.1/static/functions-string.html > > but didn't see a way. > > This function might do what you need: > > > CREATE FUNCTION spaced3 (text) RETURNS text AS $$ > DECLARE > -- Declare aliases for function arguments. > arg_string ALIAS FOR $1; > > -- Declare variables > row record; > res text; > > BEGIN > res := ''; > FOR row IN SELECT regexp_matches(arg_string, '.{1,3}', 'g') as chunk LOOP > res := res || ' ' || btrim(row.chunk::text, '{}'); > END LOOP; > RETURN res; > END; > $$ LANGUAGE 'plpgsql'; > > > # SELECT spaced3('abcdefgh'); > > spaced3 > ------------- > abc def gh > (1 row) > > # SELECT spaced3('0123456789'); > spaced3 > ---------------- > 012 345 678 9 > (1 row) > > to remove the function run this: > > # drop function spaced3(text); > > -wes Just a small optimization would be to use a backreference with regexp_replace instead of regexp_matches: select regexp_replace('foobarbaz', '(...)', E'\\1 ', 'g'); regexp_replace ---------------- foo bar baz (1 row) regards, Ken