How to check if a db field contains a non breaking space? - mysql

I import data from an old DB and want to check in my new db if a field contains a non breaking space somewhere. All solutions I found so far replace it, but I just want to search via sql in my mariaDb if in any row this field does contain the non breaking space.
Searching with like obviously doesn't work e.g.
select * from my_table
where
my_field like "%CHAR(160)%"
All solutions I found want to replace like this REPLACE(The_txt, NCHAR(160), ' ') but I want to check if I have the problem at all before messing with the db.

As already answered by #Akina, thanks for your help, the below query helps to find records with non-breaking space -
SELECT * FROM <table_name> WHERE INSTR(<column_name>, CHAR(160));
Or
SELECT * FROM <table_name> WHERE LOCATE(CHAR(160), <column_name>);

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.

Replacing some URLs with another

I have a MySQL 5.7.29 database on which a website is built. I need to construct a query to find all table rows containing lines such as
https://example.com/index.php?topic=7989.0
or similar and replace them with
https://example.com/my-redirect-page/?7989.0
The wildcard here is the ?topic=7989.0 as this can be something like ?topic=1234 or even ?topic=3456.0#anchor
I can display the rows they appear in (in PHPMyAdmin) using this (thanks to 'sticky bit' below) :
SELECT * FROM `abc_posts` WHERE `post_content` LIKE '%example.com\index.php?topic%'
My problem is that I then need to change just that URL when there is also text content around it.
Thanks in advance.
The question mark doesn't need to be escaped.
But 'https://example.com/index.php?topic=7989.0' isn't LIKE '%example.com\?topic%' as the question mark doesn't immediately follow the host name.
Try:
...
post_content LIKE '%example.com/index.php?topic%'
...
You could do something like thin to find them
SELECT 'https://example.com/index.php?topic=7989.0'
WHERE 'https://example.com/index.php?topic=7989.0' REGEXP 'example.com/index.php?topic=';
Which would find all rows. But for replacing it, you must also tell which database version youh have mysql 5.x have not many regex fucntions mariadb and mysql 8 have much more

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

MySQL - Finding partial strings - Full Text?

I just found a bunch of rogue data in my MYSQL db...
The only way to get to it is via one of the columns - FILE_PATH which contains a slash stripped version of a file path. There are a few rogue files in this set that I need find - they all have the file name "Thumbs.db" but they have a variety of paths
example:
F:DatasetGroupedByFormatsx-fmt-398Thumbs.db
I have a full text index on the field, however the following query doesn't give any returns:
SELECT * FROM main_small WHERE MATCH `FILE_PATH` AGAINST ('Thumbs.db')
Response:
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0382 sec )
I am unsure whether this is because I have the syntax wrong, or whether the text string needs to be isolated by whitespace/punctuation.
Surely it's
select * from main_small where FILE_PATH like '%Thumbs.db'
However, if not then does MySql Full text Search help?
The problem is that your query thinks 'Thumbs.db' is a whole word. You'll need to find some way to do wildcard searching in order to select those rows. How about:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'
Just use LIKE:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'

MySQL table: How can I remove specific characters from all fields?

Every week, I have to completely replace the data in several very large MySQL tables. So what I normally do is delete the existing data, import the new data, and then run my usual queries to modify the new data as needed.
Unfortunately, these days I have noticed that the new data contains unwanted characters, such as quotes and extra spaces. With well over 100,000 records in some of these tables (AFAIK), I cannot easily open the data in notepad to strip out unwanted characters, prior to importing.
I realize I could write a separate find and replace query for every single column in every table, like this:
UPDATE mytablename SET mycolumn = REPLACE(mycolumn, '"', '');
But having to name every column is a bother. Anyway, I would like to find a more elegant solution. Today, I found a snippet on the internet that looks like a start:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE
table_name = 'myTable' and ordinal_position = 1
I think the next step might be to loop through the ordinal positions, and then replace and update each column, but I don't know how to do this in MySQL. I also don't know how to stop the loop after the last column is reached, to avoid error messages.
Is there an easy way to do this? Or am I hoping for too much?
I am a beginner, so a clear, simple explanation would be much appreciated.
Thanks in advance.
MORE INFORMATION:
Since my first post, I have discovered that stored procedures are not allowed on my server. Too bad.
Anyway, I have tried this new code, just to get started:
set #mytablestring='mytable';
set #mycolumnnumber=1;
set #mycolumnname=(SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = #mytablestring and ordinal_position = #mycolumnnumber);
SELECT #mycolumnname FROM mytable;
Unfortunately, in the final SELECT query, #mycolumnname is interpreted as a string, not as a column name. So the query does not work. If I could get past this, I believe I could write some code to loop through the columns by incrementing #mycolumnnumber.
If anyone knows how to solve this, I would really appreciate some help.
Many thanks.
I suggest that you take a look at vim, sed, awk and many of the other text editors and text processing utilities that you can find on Linux (and sometimes on Windows too). 100,000 records may be a pain in Notepad, but it's a piece of cake for real text processing utilities.
For example, to strip all # characters from foobar.txt:
sed 's/#//g' foobar.txt > foobar-clean.txt
Or, the same thing with the file opened in (g)vim:
:%s/#//g