pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
find and replace the string within a column
4+ messages / 4 participants
[nested] [flat]

* find and replace the string within a column
@ 2010-09-24 09:15 Nicholas I <nicholas.domnic.i@gmail.com>
  2010-09-24 09:33 ` Re: find and replace the string within a column Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-09-24 11:34 ` Re: find and replace the string within a column Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 2 replies; 4+ messages in thread

From: Nicholas I @ 2010-09-24 09:15 UTC (permalink / raw)
  To: pgsql-sql

Hi,

the data in my table, have a substring enclosed in parenthesis,

1. i want to replace the string with the brackets to null or any other
value.
2. remove the contents within brackets.

Example:
table name person:

name
------------------------------------
Samuel (S/o Sebastin )
-------------------------------------

remove the word within the brackets.
the output should be , Samuel.

the below one help's me to find the data within the two brackets.

SELECT name,(REGEXP_MATCHES(name, E'\\(.+?\\)'))[1] from person;
regexp_matches
------------------------------------
(S/o Sebastin )
-------------------------------------

-Nicholas I

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: find and replace the string within a column
  2010-09-24 09:15 find and replace the string within a column Nicholas I <nicholas.domnic.i@gmail.com>
@ 2010-09-24 09:33 ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Jayadevan M @ 2010-09-24 09:33 UTC (permalink / raw)
  To: Nicholas I <nicholas.domnic.i@gmail.com>; +Cc: pgsql-sql; pgsql-sql-owner@postgresql.org

Hello,
> the below one help's me to find the data within the two brackets.
> 
> SELECT name,(REGEXP_MATCHES(name, E'\\(.+?\\)'))[1] from person;
> regexp_matches
> ------------------------------------
> (S/o Sebastin )
> -------------------------------------
> 
Trying to work with your code - 
update table set name = 
substr( name,1,strpos(name, (REGEXP_MATCHES(name, E'\\(.+?\\)')) )-1 ) ||
substr( name,strpos(name, (REGEXP_MATCHES(name, E'\\(.+?\\)')) ) + 1 
,char_length(name))

I am trying to find what is there before the pattern and after the pattern 
and concatenating them . Please see documentation for proper use of 
substr,strpos,cahr_length etc. 

Regards,
Jayadevan





DISCLAIMER: 

"The information in this e-mail and any attachment is intended only for 
the person to whom it is addressed and may contain confidential and/or 
privileged material. If you have received this e-mail in error, kindly 
contact the sender and destroy all copies of the original communication. 
IBS makes no warranty, express or implied, nor guarantees the accuracy, 
adequacy or completeness of the information contained in this email or any 
attachment and is not liable for any errors, defects, omissions, viruses 
or for resultant loss or damage, if any, direct or indirect."








^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: find and replace the string within a column
  2010-09-24 09:15 find and replace the string within a column Nicholas I <nicholas.domnic.i@gmail.com>
@ 2010-09-24 11:34 ` Craig Ringer <craig@postnewspapers.com.au>
  2010-09-24 14:39   ` Re: find and replace the string within a column Leif Biberg Kristensen <leif@solumslekt.org>
  1 sibling, 1 reply; 4+ messages in thread

From: Craig Ringer @ 2010-09-24 11:34 UTC (permalink / raw)
  To: Nicholas I <nicholas.domnic.i@gmail.com>; +Cc: pgsql-sql

On 24/09/2010 5:15 PM, Nicholas I wrote:

> Example:
> table name person:
>
> name
> ------------------------------------
> Samuel (S/o Sebastin )
> -------------------------------------
>
> remove the word within the brackets.
> the output should be , Samuel.

Can't you just regexp_replace, matching \([^)]+\) (in other words "an 
open parenthisis, then a sequence of one or more of any character other 
than a close parenthesis, followed by a close parentheis) and replacing 
with an empty string ?

-- 
Craig Ringer

Tech-related writing at http://soapyfrogs.blogspot.com/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: find and replace the string within a column
  2010-09-24 09:15 find and replace the string within a column Nicholas I <nicholas.domnic.i@gmail.com>
  2010-09-24 11:34 ` Re: find and replace the string within a column Craig Ringer <craig@postnewspapers.com.au>
@ 2010-09-24 14:39   ` Leif Biberg Kristensen <leif@solumslekt.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Leif Biberg Kristensen @ 2010-09-24 14:39 UTC (permalink / raw)
  To: pgsql-sql

On Friday 24. September 2010 13.34.12 Craig Ringer wrote:
> On 24/09/2010 5:15 PM, Nicholas I wrote:
> 
> > Example:
> > table name person:
> >
> > name
> > ------------------------------------
> > Samuel (S/o Sebastin )
> > -------------------------------------
> >
> > remove the word within the brackets.
> > the output should be , Samuel.
> 
> Can't you just regexp_replace, matching \([^)]+\) (in other words "an 
> open parenthisis, then a sequence of one or more of any character other 
> than a close parenthesis, followed by a close parentheis) and replacing 
> with an empty string ?

I'm doing a similar task, removing comments "hidden" within curly braces like 
this:

        str := REGEXP_REPLACE(str, '{.*?}', '', 'g');

No escaping needed at all.

regards,
Leif Biberg Kristensen




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2010-09-24 14:39 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-09-24 09:15 find and replace the string within a column Nicholas I <nicholas.domnic.i@gmail.com>
2010-09-24 09:33 ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
2010-09-24 11:34 ` Craig Ringer <craig@postnewspapers.com.au>
2010-09-24 14:39   ` Leif Biberg Kristensen <leif@solumslekt.org>

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