Add a line break using MySQL search & replace - mysql

I just migrated a SMF forum to WordPress BBPress. The problem is video urls in posts are not on their own line so WordPress shows the url instead of displaying the video.
I'd like to do a search and replace in the database and put in a return before any YouTube url.
Example:
What do you think???https://www.youtube.com/watch?v=SbbM_v2_5wA
Would become:
What do you think???
https://www.youtube.com/watch?v=SbbM_v2_5wA
How would I do this?

Try this on a backup instance of your data first as it may not do exactly what you expect; in short the below will replace every FIELD containing the string 'https://www.youtube.com/' with the same string cut by a newline preceeding the url. If you run the code twice on your data it WILL insert a second newline, which is probably not what you want.
update TABLENAME set FIELD = concat(substring(FIELD, 1, locate('https://www.youtube.com/', FIELD)-1),'\n',substring(FIELD, locate('https://www.youtube.com/', FIELD))) where locate('https://www.youtube.com/', FIELD) > 0;
You will need to change identifiers TABLENAME and FIELD to reflect your schema.

Related

How to delete a specific string value from multiple tables on phpmyadmin?

I had a customer that installed a malware plugin which has already been cleaned.
The problem now it that i have this string - https://https;//main.travelfornamewalking.ga/stat.js?s=newrq - on the 2000 posts I have on the webiste.
How can I delete this specific string on each post bulk?
Keep in mind I don't want to delete the content. The problem is the malware added a script with that link on all the entries of the wp_posts table.... :(
You have to find out the table and the column that contains the problem link.
Once you have found the table and the column, check how many rows are affected. This will also give you an overview of what you could replace the problem link with.
First, the most important thing: Make a copy of the table! If you make a wrong update, you will need it.
// Maybe just look for a part of the problem link, it could be here in another form or with other parameters, good for checking.
SELECT ColumName FROM TableName WHERE ColumName like '%main.travelfornamewalking.ga%'
To replace the problem link, use REPLACE(), e.g. overwrite with an empty string.
https://www.w3resource.com/mysql/string-functions/mysql-replace-function.php
REPLACE(str, find_string, replace_with) -> full text of problemlink with all parameters.
UPDATE TableName SET ColumName = REPLACE(ColumName, 'https://https;//main.travelfornamewalking.ga/stat.js?s=newrq', '')
-> Repeat the select and update for all forms of this problem link that you are replacing.

MySQLAdmin replace text in a field with percent in text

Using MySQLAdmin. Moved data from Windows server and trying to replace case in urls but not finding the matches. Need slashes as I don't want to replace text in anything but the urls (in post table). I think the %20 are the problem somwhow?
UPDATE table_name SET field = replace(field, '/user%20name/', '/User%20Name/')
The actual string is more like:
https://www.example.com/forum/uploads/user%20name/GFCI%20Stds%20Rev%202006%20.pdf
In a case you are using MariaDB you have REGEXP_REPLACE() function.
But best approach is to dump the table into the file. Open it in a Notepad ++
and run regex replace like specified on a pic:
Pattern is: (https:[\/\w\s\.]+uploads/)(\w+)\%20(\w+)((\/.*)+)
Replace with: $1\u$2\%20\u$3$4
Then import the table again
Hope this help
If its MariaDB, you can do the following:
UPDATE table_name SET field = REGEXP_REPLACE(field, '\/user%20name\/', '\/User%20Name\/');
First, please check, what is actually stored in the database: %20 is a html-entity which represents a whitespace. Usually, when you are storing this inside the database, it will be represented as an actual whitespace (converted before you store it) -> Hence your replace doesn't match the actual data.
The second option that might be possible - depending on what you want to do: You are seeing the URL containing %20, therefore you created your database records (which you would like to fetch) with that additional %20 - And when you now try to query your results based on the actual url, the %20 is replaced with an "actual" whitespace (before your query) and hence it doesn't match your stored data.

How do you remove random text in a field in a mySQL table using phpMyAdmin

I've been working with a company that is half-way around the world converting a database. They put a random set of text at the end of text inside columns in a table and I want to remove the text as easily as possible. I don't want to hand-remove thousands of entries.
For example, I have a table called "Users".
The table "users" have many different fields, but the one I want to modify is called "username"
For that column, we have the following entries:
Sam-ABC123
Bob-ABC234
Joe-ABC578
I could go into each row and change each entry so that it shows
Sam
Bob
Joe
But I have 4,000 of these just on that field alone. Quite time consuming and prone to error!
I've tried going into phpMyAdmin and do a search and replace but nothing changed.
I tried:
Find: ABC%
REPLACE: def
Column: username
Preview shows all the tables, but the original string and replacement string are the same; nothing gets replaced
I tried:
Find: ABC*
REPLACE: def
Column: username
Preview shows nothing.
Thanks in advance!
Since phpMyAdmin allows you to execute any query, why don't you just do
UPDATE users SET username = SUBSTRING(username, 0, INSTR(username, '-') - 1);
Since the dash character is used as a separator you can use a SQL query and SUBSTRING_INDEX:
UPDATE users
SET username = SUBSTRING_INDEX(username, '-', 1)
Please see a fiddle here.

Replace not working

I have a text-file of data in key-value pairs that I have managed to convert to a format where the key-value pairs are all separated by an underscore between them, and the key is separated from the value by a colon. I thought this format would be useful for keeping spaces intact within the data. Here's an example with the data substituted for ~~~~~~~s.
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~ ...etc
I want to convert this to a MySQL script to insert the data into a table. My problem is there are nullable fields that aren't included in every record. e.g. A record has a _TYPE1: and may or may not have a _TYPE2:
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
I thought to fix this by inserting _TYPE2: after every _TYPE1 without a _TYPE2:. Since there are only a few different possible types, I managed to select the _ after each _TYPE1:~~~~~~ without a TYPE2: following it. I used the following regex, where egtype is one example of a possible type:
(?<=_TYPE1:egtype)_(?!TYPE2:)
At this point, all I have to do is replace that _ with _TYPE2:_ and every field is present in every line, which makes it easy to convert every row to a MySQL insert statement! Unfortunately, Notepad++ is not replacing it when I click the Replace button. I'm not sure why.
Does anyone know why it wouldn't replace an _ with _TYPE2:_ using that particular regex? Or does anyone have any other suggestions on how to turn all this data into a MySQL insert script?
Regex
To do what you want, try this:
Find:
_TYPE1:[^_]+\K(?!.*_TYPE2)
Replace:
_TYPE2:
You can test it with your sample data and have it explained here.
Python Script plugin
As a side note, I don't think it's possible to convert your data into SQL insert statements with the use of one and only one regular expression, and while I see what you are trying to do by adding fake TYPE2, I don't think it is your best option.
So, my suggestion is to use Notepad++'s Python Script plugin.
Install Python Script plugin, from Plugin Manager or from the official website.
Then go to Plugins > Python Script > New Script. Choose a filename for your new file (eg sql_insert.py) and copy the code that follows.
Run Plugins > Python Script > Scripts > sql_insert.py and a new tab will show up the desired result.
Script:
columns = [[]]
values = [[]]
current_line = 0
def insert(line, match):
global current_line
if line > current_line:
current_line += 1
columns.append([])
values.append([])
if match:
i = 0
for m in match.groups():
if i % 2 == 0:
columns[line].append(m)
else:
values[line].append(m)
i += 1
editor.pysearch("_([A-Z0-9]+):([^_\n]+)", insert)
notepad.new()
for line in range(len(columns)):
editor.addText("INSERT INTO table (" + ",".join(columns[line]) + ") values (" + ",".join(values[line]) +");\n")
Note: I'm still learning Python and I've a feeling that this one could be written in a better way. Feel free to edit my answer or drop a comment if you can suggest improvements!
Example input:
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~
Example output:
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,TYPE2) values (~~~,~~~~~,~~~~~~~,~~~~~~,~~~~~~);
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,TYPE2,ADDRESS) values (~~~,~~~~~,~~~~~~,~~~~~~,~~~~~~,~~~~~~~);
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,ADDRESS) values (~~~,~~~~~,~~~~~~,~~~~~~,~~~~~~~);
try searching for (_TYPE1:)(\S\S\S\S\S\S)(_ADDRESS:)
and replacing with \1\2_TYPE2:~~~~~~\3
i tested in notepad++ with your data and it works
don't forget to change the Search Mode to regular expression.
to turn it into an INSERT script just keep using regular expression like i did above, and bracket which ever field you want and then replace with a \number whichever field and move them around it should be pretty simple manual labor, have fun.
for example search for your whole line here i am only doing DESCRIPTION,TYPE1,and TYPE2
search for using regular expression
(_DESCRIPTION)(:)(\S\S\S\S\S\S)(_TYPE1)(:)(\S\S\S\S\S\S)(_TYPE2)(:)(\S\S\S\S\S\S)
then replace with something like
INSERT INTO table1\(desc,type1,type2\)values\('\3','\6','\9'\); (in notepad++)
If this is a once-off problem then a two step process would work. First step would add a _TYPE2:SomeDefaultValue to every line. Step two would remove it from lines where it was not needed.
Step 1: Find what: $, Replace with: _TYPE2:xxx
Step 2: Find what: (_TYPE2:.*)_TYPE2:xxx$, Replace with: \1
In both steps select "regular expression" and un-select "dot matches newline". Also change xxx to your default value.

mysql find / replace characters at start and end of a string, but not middle of string

Short version:
I need mysql code that will change
[href="http://a-random-domain.com"]hyperlink[/href]
into
hyperlink
without messing up the domain part, and
without accidentally converting any unrelated instances of "] that might occur in the text of a field.
Domain will be unique/different every time it is
encountered.
Long version:
I am migrating an old database for use with a new application. The old database has a text field that includes content such as:
This is a data field with a [href="http://somedomain.com"]hyperlink[/href] and more data and possibly other hyperlinks.
I need to update it to standard html, e.g.
This is a data field with a hyperlink and more data and possibly other hyperlinks.
Fixing the [href= and [/href] is simple enough using REPLACE
update table set field = replace(field, '[href=', '<a href=');
but I get tripped up on the "] closing bracket of the a href tag. And in the database there are other instances of "] that shouldn't be modified, so I can't just replace on "]
Does mysql have some sort of regex "lookahead" or other way to accomplish this?
Thanks much!
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
MySQL supports regex-functions for 'evaluating' purpose only.
So, you can't use it on replacing or manipulating data. It'll be better using high level script language for it.