Remove unwanted characters in a string using regex - mysql

I have a string like this in my column:
<p>[img ret872154ftu] fileaddress [/img ret872154ftu]</p>
<p>[img fd68721cvn] fileaddress [/img fd68721cvn]</p>
<p>[img xdfh654t] fileaddress [/img xdfh654t]</p>
Now i wish to remove unwanted chars inside [img] and [/img].
I have already used this query but does not work:
UPDATE `table` SET `content` = replace(`content`, '[img [^]*]', '[img]');
Any suggestion?

UPDATE table SET
column=REPLACE(column,SUBSTRING_INDEX(SUBSTRING_INDEX(column,'/img ',-1),']',1),'');
UPDATE table SET
column=REPLACE(column,SUBSTRING_INDEX(SUBSTRING_INDEX(column,'[img ',-1),']',1),'');
SQL Fiddle
2 updates,with different delimiters because the texts might be different.

Related

Remove last char if it's a specific character

I need to update values in a table by removing their last char if they ends with a +
Example:
John+Doe and John+Doe+ should both become John+Doe.
What's the best way to achieve this?
UPDATE table
SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)
WHERE field LIKE '%+'
If you are trying to display the field instead of update the table, then you can use a CASE statement:
select
case
when right(yourfield,1) = '+' then left(yourfield,length(yourfield)-1)
else yourfield end
from yourtable
SQL Fiddle Demo
you didn't explain exactly the situation.
but if you search for names in a text. I'll remove all the non chars (anything not a-z and A-Z) including spaces and then compare.
if you want just the last char, try the SUBSTRING_INDEX function.
if you are passing to the DB as a string, you can do this with str_replace
<?php
$str = "John+Doe+";
$str = str_replace("+"," ",$str);
echo $str;
?>

Incrementing numerical value and changing string in SQL

I have a database that has stored values in a complicated, serialized array where one component is a string and another is the length of the characters of the string, in this format:
s:8:"test.com"
Where "s" holds the character length of the string in the quotations.
I would like to change the string from "test.com" to "testt.com", and I'm using the following statement in SQL:
UPDATE table SET row=(REPLACE (row, 'test.com','testt.com'))
However, this breaks the script in question, because it doesn't update the character length in the "s" preceding the string where "test.com" is stored.
I was wondering if there is a query I can use that would replace the string, and then also increment the value of this "s" preceding to where the replacement occurs, something like this:
UPDATE table SET row=(REPLACE (row, 's:' number 'test.com','s:' number+1 'testt.com'))
Does anyone know if this kind of query is even possible?
UPDATE table set row = concat('s:',length('testt.com'),':"testt.com"');
If you need to change exact string, then use exact query -
UPDATE table SET row = 's:9:"testt.com"' WHERE row = 's:8:"test.com"';
The string is a "serialized string".
If there are multiple strings to be replaced, it might be easier to create a script to handle this.
In PHP, it goes something like this:
$searchfor = serialize('test.com');
$replaceby = serialize('testt.com');
// strip last semicolon from serialized string
$searchfor = trim($searchfor,';');
$replaceby = trim($replaceby,';');
$query = "UPDATE table SET field = '$replaceby' WHERE field = '$searchfor';";
This way, you can create an exact query string with what you need.
Do fill in the proper code for db connection if necessary.

mysql replace last characters in string if matched

I have a table that has some rogue tags that need replacing
The offending string ends <tr> and needs replacing with </table>
Not all record are affected, so I need to find these and then replace them
Our skills using Update Replace Where are limited as the characters are not unique within the string but their position is, ie the last 4 characters
Have tried using
UPDATE table
SET field
REPLACE (RIGHT(field,4),</table>)
but suspec this is over simplified (and also fails)
try this:
UPDATE table
SET field=concat(left(field,length(field) -4),'</table>')
I had a similar situation in which needed to replace '_' from end of the transaction number field, where there where more than one occurrences of _ in field. Example: 20161124_C_BGN_5570.77_ & 20161121_C_HRK_1502360000__
Solution:
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -1)
WHERE RIGHT(transaction, 1) = '_';
// in case of double underscore (__)
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -2) # WHERE id = xxx WHERE RIGHT(transaction, 2) = '__';

MySQL Replace Syntax -- wrap specific piece of text

Within a table, there is a params row that has some JSON encoded data -- as such:
{"categories":211,"singleCatOrdering":"","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"","show_page_heading":0,"page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}
What I need to do is wrap [] around the categories parameter - to look as such:
{"categories":[211],"singleCatOrdering":"","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"","show_page_heading":0,"page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}
I have tried the following (and a bunch of other failed ones) with no-dice:
UPDATE j17_menu SET params = REPLACE(params,'"categories":%,','"categories":[%],') WHERE component_id = 10021;
Am I possibly using the wildcard option wrong? Any nudges in the right direction would be a huge help. Thanks!
This one will work independent of the context
UPDATE j17_menu
SET params=CONCAT (
SUBSTR(params,1,LOCATE('"categories":',params)),
'"categories":[',
substr(
params,
LOCATE('"categories":',params)+13,
LOCATE(',',params,LOCATE('"categories":',params))-LOCATE('"categories":',params)-13
),
']',
substr(params,LOCATE(',',params,LOCATE('"categories":',params)))
)
WHERE component_id = 10021;

Mysql cut string, the first character?

Hi is it possible to cut string like this:
String in "data" columns: ,123,456
Cut the first character i.e "," (comma).
So the query is something like:
Update users set data = cut first string...
UPDATE users SET data = SUBSTR(data, 2);
This will iterate through all rows in users and replace data with itself minus the first character.