Connecting Tech Pros Worldwide Help | Site Map

updating substrings in mysql

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:35 AM
kaptain kernel
Guest
 
Posts: n/a
Default updating substrings in mysql

how does one go about updating a substring in mysql
e.g.

I want to change all occurences of Gmbh to GMBH within a field string.

BMW gmbh
BMW Gmbh
Mercedes Gmbh
Joe Blogg Gmbh & Mercedes Ltd

get changed to

BMW GMBH
BMW GMBH
Mercedes GMBH
Joe Blogg GMBH & Mercedes Ltd

I know how to do this in PHP, but is there a way of doing it in MySQL
natively?


  #2  
Old July 17th, 2005, 01:35 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: updating substrings in mysql

kaptain kernel wrote:[color=blue]
> I want to change all occurences of Gmbh to GMBH within a field string.[/color]

Maybe like this:


-- SQL --
create table `test`.`xx` (
`id` int (2) UNSIGNED NOT NULL AUTO_INCREMENT ,
`description` varchar (32) NULL ,
PRIMARY KEY ( `id` )
)


insert into `test`.`xx` (description) values
('BMW gmbh'), ('BMW Gmbh'), ('Mercedes Gmbh'), ('Joe Blogg Gmbh &
Mercedes Ltd')


select * from xx


update xx
set description = INSERT(description, INSTR(description, 'gmbh'), 4,
'GMBH')
where LOCATE('gmbh', description)>0


select * from xx
-- END SQL --



HTH
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
  #3  
Old July 17th, 2005, 01:36 AM
kaptain kernel
Guest
 
Posts: n/a
Default Re: updating substrings in mysql

That is most excellent - thanks. i wasn't aware of the LOCATE function.
This will save a LOT of time (and PHP processing...)



Pedro Graca wrote:[color=blue]
> kaptain kernel wrote:[color=green]
>> I want to change all occurences of Gmbh to GMBH within a field string.[/color]
>
> Maybe like this:
>
>
> -- SQL --
> create table `test`.`xx` (
> `id` int (2) UNSIGNED NOT NULL AUTO_INCREMENT ,
> `description` varchar (32) NULL ,
> PRIMARY KEY ( `id` )
> )
>
>
> insert into `test`.`xx` (description) values
> ('BMW gmbh'), ('BMW Gmbh'), ('Mercedes Gmbh'), ('Joe Blogg Gmbh &
> Mercedes Ltd')
>
>
> select * from xx
>
>
> update xx
> set description = INSERT(description, INSTR(description, 'gmbh'), 4,
> 'GMBH')
> where LOCATE('gmbh', description)>0
>
>
> select * from xx
> -- END SQL --
>
>
>
> HTH[/color]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.