SQL Find & Replace part of a string - mysql

I'm trying to find certain text "catid=18" in a string, where each string is different except for this.
I've used this query below before, except it only seems to work if you know the entire string.
update TABLE_NAME
set FIELD_NAME = replace(FIELD_NAME, 'findthis', 'replacewiththis');

Not sure if that is what you want. But it will return 1 if catid=any_num is found and 0 if not:
select 'some_text catid=18 some_text' REGEXP 'catid=[0-9]+'

Maybe you need:
update TABLE_NAME
set FIELD_NAME = 'goodvalue'
WHERE FIELD_NAME = 'badvalue';

Related

How to remove string and what comes after it?

I have a column with links in my table. Some links have a string like /?source=rss&....
So I want to remove this string /?source=rss and what comes after it.
So if the link looks like this https://website.com/?source=ress&id=123&name=john
It should be https://website.com
I searched about it but only found replace and remove functions, And they won't work as the string is variable and not the same in every link.
I found this UPDATE MyTable SET column = REPLACE(column, ' ', '') WHERE column LIKE '%%'
The database is MYSQL and I'm using SQL queries there
Use substring_index():
UPDATE MyTable
SET column = SUBSTRING_INDEX(column, '/?', 1)
WHERE column LIKE '%/?%';

MySQL phpMyAdmin replace all like pattern in multiple tables

I am trying to look for a way to replace all occurrences of a particular pattern in my database across many tables and columns, for this I need to create some way to do this, it does not need to be done by a script, just some SQL code that will do this.
For example, I want to replace all occurrences of 'v2' with 'www' but have no idea how to do this.
I am not looking for a tutorial, just a bit of guidance on what to do and how to script the SQL needed.
How do I go about doing this?
Just to guide you in a direction. You can use the replace function:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%';
In an earlier post there is more information:
How can I use mySQL replace() to replace strings in multiple records?
You can use a select CONCAT(...) from information_schema.columns to generate an update query for every table-column combination, where ... is a combination of the strings used in an update query, and column names of the information_schema.columns database.
For example:
select CONCAT("UPDATE ", TABLE_NAME, " SET ", COLUMN_NAME, "=REPLACE(",COLUMN_NAME,"'[string-to-find]'","'[string-that-will-replace-it]'",");") FROM information_schema.columns where table_schema = 'your_db';
COLUMN_NAME and TABLE_NAME are columns in the information_schema.columns table, as documented by MySQL
The above query should make the result set:
UPDATE table1 SET field1 = replace(field1,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table1 SET field2 = replace(field2,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table2 SET field3 = replace(field3,'[string-to-find]','[string-that-will-replace-it]');
...
You could output the results of the SELECT statement to a file, which then becomes a script to execute. Alternatively, if you use phpadmin or any other programming language as an interface, you can cycle through the results set, and execute the value of each row in the results set.
I got the idea from MikeW's answer here, about selecting all rows where a data value exists, and from some other stack overflow answers that I have now lost track of, sadly (sorry to the original writers)
To be honest, I think this question may be a duplicate of this, in addition to this though...
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'Text to replace it with');

how can use a wildcard in middle of a sub string that I dont know its length?

I am trying to make an update on a DB replacing old characters for new ones, my problem is that at the middle of that string I have some characters I have to remove but I dont know the length or the type, I just know when the sub string starts and when it ends, for exmaple
my-string=XXXXXXXX-endstring
so, Id like to make it like this
new-string-endstring
I know my-string value, but dont know the sub string in the middle XXXXX
is there any way to do it?
Thanks.
Update fields which begin with my-string- AND end with -endstring:
UPDATE table_name
SET column_name = 'new-string-endstring'
WHERE column_name LIKE 'my-string-%'
AND column_name LIKE '%-endstring';
If you use PHP you could do the following:
1) Make a SELECT of all rows that have the my-string text inside the column value
SELECT * FROM table_name WHERE column_name LIKE '%my-string%'
the % acts as a wildcard
2) Loop through all these result rows and use PHP's str_replace to replace my-string for new_string, and then use MySQL's UPDATE to update the new value of the column in the DB
You can use SUBSTRING_INDEX to get the required string and use CONCAT to append new string.
Update TableA
Set columnA = CONCAT( 'newstring' , SUBSTRING_INDEX(columnA, '-', -1) )

Needing to do a simple Find/Replace in SQL

I'm needing to do a very important database string correction on 192 rows and am wondering if this is correct syntax:
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` = REPLACE(`FIELD_NAME`,`REPLACE_THIS_STRING`,`WITH_THIS_STRING`);
Thanks in advance!
The best way to find out is to write it as a SELECT statement first to "preview" the results.
SELECT field_name As before
, Replace(field_name, 'replace this string', 'with this string') As after
FROM table_name
Optional WHERE clause (to only affect the rows that contain our replacement string):
...
WHERE field_name LIKE '%replace this string%'
Well, i would write it like this
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` =
REPLACE(`FIELD_NAME`,`REPLACE_THIS_VALUE`,`WITH_THIS_VALUE`);
Does the table only contain these 192 rows? Otherwise you must add a WHERE to the syntax. Or it will update all FIELD_NAME rows.
As #gvee suggest, try to select the rows first and see how the result looks like to ensure the update scope is correct

Mysql query that replaces a unknown number in a path

Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo