Thread: Insert a space between each character

Insert a space between each character

From
"Nicholas I"
Date:
<div dir="ltr">Hi, <br /><br /> can anybody help me, to insert a space between each character in postgresql.<br /><br
/> forexample,<br /><br />  ABC<br /><br /> output<br />  A B C<br /><br />-Dominic<br /></div> 

Re: Insert a space between each character

From
"A. Kretschmer"
Date:
am  Wed, dem 17.09.2008, um 11:49:27 +0530 mailte Nicholas I folgendes:
> Hi,
> 
>  can anybody help me, to insert a space between each character in postgresql.
> 
>  for example,
> 
>   ABC
> 
>  output
>   A B C

write a function in plpgsql, you can use string-function like substr to
split the string. Write a loop over the string, pick up every char, and
concatenate a space. Return the new string.


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net


Re: Insert a space between each character

From
Michael Toews
Date:
Hi,

Use a regular expression, e.g.:

select trim(regexp_replace('foobarbaz', '(.)', E'\\1 ', 'g'));

See http://www.postgresql.org/docs/8.3/static/functions-matching.html
for more.
+mt

Nicholas I wrote:
> Hi,
>
>  can anybody help me, to insert a space between each character in
> postgresql.
>
>  for example,
>
>   ABC
>
>  output
>   A B C
>
> -Dominic



Re: Insert a space between each character

From
"Dirk Jagdmann"
Date:
> Use a regular expression, e.g.:
> select trim(regexp_replace('foobarbaz', '(.)', E'\\1 ', 'g'));

And if we only match characters until the last character in the
string, we can get rid of the outer trim():

# select regexp_replace('foobarbaz', E'(.)(?!$)', E'\\1 ', 'g'); regexp_replace
-------------------f o o b a r b a z
(1 row)

-- 
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org