Unix Technical Forum

Replacing ONLY the first occurence of a substring in a string

This is a discussion on Replacing ONLY the first occurence of a substring in a string within the MySQL forums, part of the Database Server Software category; --> I have a field which has multiple comma characters in it and I want to change the first occurrence ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:32 AM
mcl
 
Posts: n/a
Default Replacing ONLY the first occurence of a substring in a string

I have a field which has multiple comma characters in it and I want to
change the first occurrence only.

I can not work out how to do this as replace changes all occurrences.

eg

Field
'fred, bob, june, 12/34/56'
needs to become
'fred^ bob, june, 12/34/56'

where the first comma ',' becomes a circumflex '^'

The first comma can be in any position or not present at all

I imagine it will be something like

set field1= IF(POS(field1,','), CONCAT(substr field1 up to comma, '^',
substr field1 after comma),else field1 Unchanged)

If someone could help before I wipe my whole field I would be most
grateful.

Richard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:32 AM
Luuk
 
Posts: n/a
Default Re: Replacing ONLY the first occurence of a substring in a string


"mcl" <mcl.office@googlemail.com> schreef in bericht
news:dd05f666-41df-4c01-8473-7fb91e9a7634@z17g2000hsg.googlegroups.com...
>I have a field which has multiple comma characters in it and I want to
> change the first occurrence only.
>
> I can not work out how to do this as replace changes all occurrences.
>
> eg
>
> Field
> 'fred, bob, june, 12/34/56'
> needs to become
> 'fred^ bob, june, 12/34/56'
>
> where the first comma ',' becomes a circumflex '^'
>
> The first comma can be in any position or not present at all
>
> I imagine it will be something like
>
> set field1= IF(POS(field1,','), CONCAT(substr field1 up to comma, '^',
> substr field1 after comma),else field1 Unchanged)
>
> If someone could help before I wipe my whole field I would be most
> grateful.
>
> Richard



set field1= REPLACE(field1,LEFT(field1,LOCATE(',',field1)),
CONCAT(LEFT(field1,LOCATE(',',field1)-1),'^'))

of course with the check added that there should be a ',' in your `field1`


--
Luuk


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:32 AM
mcl
 
Posts: n/a
Default Re: Replacing ONLY the first occurence of a substring in a string

On Jan 16, 12:51 pm, "Luuk" <l...@invalid.lan> wrote:
> "mcl" <mcl.off...@googlemail.com> schreef in berichtnews:dd05f666-41df-4c01-8473-7fb91e9a7634@z17g2000hsg.googlegroups.com...
>
>
>
> >I have a field which has multiple comma characters in it and I want to
> > change the first occurrence only.

>
> > I can not work out how to do this as replace changes all occurrences.

>
> > eg

>
> > Field
> > 'fred, bob, june, 12/34/56'
> > needs to become
> > 'fred^ bob, june, 12/34/56'

>
> > where the first comma ',' becomes a circumflex '^'

>
> > The first comma can be in any position or not present at all

>
> > I imagine it will be something like

>
> > set field1= IF(POS(field1,','), CONCAT(substr field1 up to comma, '^',
> > substr field1 after comma),else field1 Unchanged)

>
> > If someone could help before I wipe my whole field I would be most
> > grateful.

>
> > Richard

>
> set field1= REPLACE(field1,LEFT(field1,LOCATE(',',field1)),
> CONCAT(LEFT(field1,LOCATE(',',field1)-1),'^'))
>
> of course with the check added that there should be a ',' in your `field1`
>
> --
> Luuk


Many thanks - worked a treat, but it took me a while to work out what
was involved

I guess the old grey matter is just not what it was.

Richard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:43 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com