The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Replace values between brackets
Discuss Replace values between brackets in the MySQL Help forum on Dev Shed. Replace values between brackets MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 29th, 2013, 01:07 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
|
Replace values between brackets
Good day,
I am busy trying to port over a BBforum to another system, and everything ported over well, except for the images. The images are in [img] brackets, while the new system use simple html. In the db, the img brackets are encoded with random magic quotes, for example [img:2it3n9tl]. with the magic quotes being random, I will have to find and replace every one of over 3000 posts. I am looking for a SQL query I can run that will replace the values in square brackets with something else. Is there a way one can use a SQL query that will replace everything between the brackets? I will really appreciate all the help.
Best Regards,
Jakes
|

January 29th, 2013, 05:06 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by Jakes79 In the db, the img brackets are encoded with random magic quotes, for example [img:2it3n9tl]. | what magic quotes? i don't see no magic quotes
but to answer your question, no there is no sql that will replace everything between the brackets
|

January 29th, 2013, 07:24 AM
|
|
Contributing User
|
|
Join Date: Mar 2012
Posts: 43
Time spent in forums: 8 h 26 m 15 sec
Reputation Power: 2
|
|
Hi,
I'm also not sure what you mean by magic quotes, but if you are referring to the square brackets you can use find_in_set() or locate() to find the starting bracket and ending bracket and then change the substring in between.
for that, this may help:
https://launchpad.net/mysql-udf-regexp
if you don't want to do that, you'd probably have to do something like:
Quote: update img_nameline
set img_nameline = concat( left(img_nameline, locate('[',img_nameline)), whatever, right(img_nameline,locate(']',img_nameline)) ) |
Last edited by bobert123 : January 30th, 2013 at 01:44 AM.
|

January 30th, 2013, 04:45 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
|
Hi all, thanks for replying. Ok, my apologies,
refering to magic quotes, I am refering to the values following the [img tags. They do not show up when viewed on the forum, but only in the database. My understanding is that it is magic quotes, randomely coded for security reasons? My apologies if I am wrong.
@bobert, I will test out your suggestions, and give feedback. Thanks a million!
|

January 30th, 2013, 06:48 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
Ok, tested your method, with no luck. It simply places the existing text in brackets (unless I am doing something wrong)
To start with: My table name is forum_post and the column name is text
Here is how I've tried it:
Quote: | update ow_forum_post set text = concat( left(text, locate('[',text)), text, right(text,locate(']',text)) ) |
I also did some research, and tried this:
Quote: | update ow_forum_post set columnName = (SELECT REPLACE(STUFF([ColumnName],1,text('[',[columnName]),''),']','')) where [columnName] like '%@%' select * Table"]update ow_forum_post set columnName = (SELECT REPLACE(STUFF([ColumnName],1,text('[',[columnName]),''),']','')) where [columnName] like '%@%' select * Table |
The thing is, I have very limited knowledge on SQL, and by trying and asking,I am learning. If someone can help me I will greatly appreciate it!
|

January 30th, 2013, 08:20 AM
|
|
Contributing User
|
|
Join Date: Mar 2012
Posts: 43
Time spent in forums: 8 h 26 m 15 sec
Reputation Power: 2
|
|
|
Hi Jakes,
what is it that you'd like there to be between the brackets?
|

January 30th, 2013, 08:48 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
|
Well, just the word image. so it must be [image] instead of [img- and the various numbers]
|

January 30th, 2013, 08:57 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
|
STUFF is microsoft sql server, not mysql
please confirm which database engine you're using
|

January 30th, 2013, 09:01 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
|
I am using MySQL
Thanks!
|

February 3rd, 2013, 08:45 AM
|
|
Contributing User
|
|
Join Date: Mar 2012
Posts: 43
Time spent in forums: 8 h 26 m 15 sec
Reputation Power: 2
|
|
Quote: | Originally Posted by Jakes79 Well, just the word image. so it must be [image] instead of [img- and the various numbers] |
in that case, try running your query with this:
Quote: | update ow_forum_post set text = concat( left(text, locate('[',text)), "image", right(text,locate(']',text)) ) |
|

February 4th, 2013, 02:33 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 59 m 48 sec
Reputation Power: 0
|
|
Hi there,
Thanks for the reply. It work, but now I have the issue, when I have other square brackets too, it replaces everything between the first and last bracket.
for example, I have [img:8787] imagepath1[/img]
the result is {image} instead of {image} imagepath1{image}
here is the query I've used:
Quote: | update ow_forum_post set text = concat( left(text, locate('[',text)), "{image}", right(text,locate(']',text)) ) |
|

February 10th, 2013, 03:30 AM
|
|
Contributing User
|
|
Join Date: Mar 2012
Posts: 43
Time spent in forums: 8 h 26 m 15 sec
Reputation Power: 2
|
|
Quote: | Originally Posted by Jakes79 Hi there,
Thanks for the reply. It work, but now I have the issue, when I have other square brackets too, it replaces everything between the first and last bracket.
for example, I have [img:8787] imagepath1[/img]
the result is {image} instead of {image} imagepath1{image}
here is the query I've used: |
what would you prefer that this become?
[img:8787] imagepath1[/img]
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|