Remove html link from mysql database field with SQL - mysql

I have an old MySQL database and I need to remove different HTML links from description field.
So, for example after SQL execution on description with a following text:
Hello test World !
I need to get:
Hello World !
Please note that in my database HTML links are not the same and contain different addresses and texts.
Is it possible to do with MySQL SQL query and if so, could you please provide an example.

You can use a query like this. You only must change FIELDNAME to your fieldname and TABLENAME to your tablename. In your Sample there are one space behind HELLO ** and one before ** WORLD, so you have 2 spaces in the RESULT
SELECT
CONCAT(
SUBSTR(FIELDNAME,1,
INSTR(FIELDNAME,'<a href=')-1)
,
SUBSTR(FIELDNAME,
INSTR(FIELDNAME,'</a>')+4)
)
FROM YOURTABLE;
sample
SELECT
CONCAT(
SUBSTR('Hello test World !',1,
INSTR('Hello test World !','<a href=')-1)
,
SUBSTR('Hello test World !',
INSTR('Hello test World !','</a>')+4)
);
result
Hello World !

Related

MYSQL Remove string between two known strings

I have a WordPress website and following a change of theme I have to modify the structure of a thousand links present on about fifty pages. I would like to do it in SQL via PHPMYADMIN.
Is there a way in SQL to remove the end of all my link with the following structure :
- <a href="https://website.com/cours/les-problemes/lecon/s1-2014-2015-mathematiques-les-problemes/">
- <a href="https://website.com/cours/la-division/lecon/s3-2014-2015-mathematiques-la-division-n-nakatani/">
- <a href="https://website.com/cours/mathematiques-larithmetique/lecon/201819-s5-fa-mathematiques-nathalie-nakatani/">
In order to only get :
- <a href="https://website.com/cours/les-problemes/">
- <a href="https://website.com/cours/la-division/">
- <a href="https://website.com/cours/mathematiques-larithmetique/">
I tried to use the answer of this topic : MYSQL Replace string between two known strings but I did not manage to find a solution to fit my purpose.
I also thought about doing it in two parts :
1- Remove the content between the '/lecon/' and the '">'.
2- Then remove completely all the iteration of '/lecon/' of my pages, because they only occur on the links that I want to edit.
But my knowledges in SQL are limited and I have no clue of how to do the first part.
My apologizes for my English.
Thanks in avance for any helps !
On MySQL 8+, we can try using a regex replacement:
SELECT
tag,
REGEXP_REPLACE(tag, '(<a href="https?://(?:[^/]+/){3}).*">', '$1">')
FROM yourTable;
Demo
For MySQL < 8 (5.7), without REGEXP_REPLACE:
SELECT REPLACE(CONCAT(TRIM(TRAILING SUBSTRING_INDEX(url, '/lecon/', -1) FROM url), '">'), 'lecon/', '') FROM `your_table`
DEMO
Using your idea, I removed all from /lecon/ to the end in STEP 1 and concatenated "> to repair the HTML URL, and then I replaced lecon/ with an empty string in STEP 2.

MySQL SUBSTR LOCATE multi-search-strings

Tricky one, and my brain is mush after staring at my screen for about an hour.
I'm trying to query my database to return the first part of a string (domain name eg. http://www.example.com) in the column image_link.
I have managed this for all rows where the image_link contains .com as part of the string... but I need the code to be more versatile, so it searches for the likes of .net and .co.uk too.
Had thought some sort of nested REPLACE might work, but it doesn't make sense when I try to apply it - and I'm stuck.
Query Builder code:
$builder->select("SUBSTRING(image_link, 1, LOCATE('.com', image_link) + 3) AS domain");
Example strings, with desired results:
http://www.example.com/brands/567.jpg // http://www.example.com
https://www.example.org/photo.png // https://www.example.org
http://example.net/789 // http://example.net
Any help/advice warmly welcomed!
SELECT ... ,
SUBSTRING_INDEX(image_link, '/', 3) domain
FROM test;
Or, if protocol may be absent, then
SELECT ... ,
SUBSTRING_INDEX(image_link, '/', CASE WHEN LOCATE('//', image_link) THEN 3 ELSE 1 END) domain
FROM test;
fiddle

Mysql Query Request

Straight to the point and this might be very simple for some of you.
I have a simple SELECT query (select description from table) which produces all i want like below :
- testword123
- testword875
- myjob1 45
- myjob is 544
What i need is to have as a result :
- testword
- myjob
I can use a SELECT distinct LEFT(description,8) which works fine, but the problem is not ALL 'description' have the same number of words :-(
So basically, what i want is retrieve ONLY the letters from the 'description' result set.
Thanks!!
R
SELECT distinct LEFT(description, charindex(' ', description) - 1)
Depending on your implementation, it might be possible to declare 'description' as a variable beforehand so you don't have to type it twice in the same query.
There are two decisions:
1) Handle each decription in PHP
or
2) Handle user input before writing it to DB. Add field to table as index of first not letter symbol and then use it in LEFT mysql function
Thanks "undefined_variable" - Your solution "stackoverflow.com/questions/11134452/…; was the correct one!! (y) (with a little bit of tweaking, this helped A LOT) A+++

SQL Injection on BadStore

I'm trying to excercise on BadStore, for those who don't know it's a fake online store site which can be run on VM box, and offers a lot of security vulnerabilities.
One thing i'm trying to do is to apply sql injection on the search query.
When searching for "book", for instance, we see this:
So, i'm trying to show all the store items trying to search for 1=1' --, which will result with the query of:
SELECT itemnum, sdesc, ldesc, price FROM itemdb WHERE '1=1' --' IN (itemnum,sdesc,ldesc)
however, this not giving the expected outcome as I get the following error:
Any suggestions?
You realize that -- in MySQL acts as a comment for the rest of the line?
If this is what you are trying to do, commenting out the rest of the line, then as per the MySQL documentation, you need a space after the --.
I understand you are trying out MySQL injection, so try to type your query, and then after the query type ; -- Notice that there IS a trailing space.
TL;DR
Change
'1=1' --' IN
TO
'1=1' -- ' IN

Query first sentence of an HTML paragraph field in SQL Server / Access front end

I have been trying to find how I can query the first sentence only of a paragraph within a field (HTML code) for SQL Server but I am unable to find how. I have found solutions for MySQL using the SUBSTRING_INDEX and I have also found solutions returning a certain number of words/characters but not using a specific delimiter.
My field is stored as HTML, an example is as follows:
<html><body>Enter the following page information.<br><br>
<b>Display #:</b> 1 [Automatically Populated]<br>
<b>Start Page: </b> 1 [Automatically Populated]<br>
<b>DCI Name:</b> DEMOG<br>
<b>Clinical Planned Event:</b> BASELINE1<br>
<font color="#0070C0">TAKE A SCREENSHOT</font>.<br>
</body></html>
In this example, I am hoping to only return/query "Enter the following page information" and not the rest of the paragraph. I'm assuming the HTML break might be the best delimiter as some sentences may end in a colon.
Thank you in advance! I hope I explained the scenario well enough.
I realize this is ugly as sin, but assuming that the first <br> is the end of the line, this should work in the SQL Server back-end:
DECLARE #x nvarchar(200)
SET #x = '<html><body>Enter the following page information.<br><br><b>Display #:</b>'
SELECT substring(#x,
(charindex('<br>', lower(#x)) - 1) -
(charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))))+2,
charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))) - 1
)
Basically, we find the last instance of > in the string before the first <br>, and then find the <br> at the end, and take the difference between the two for the length.
This could absolutely be written cleaner in a function, but I opted to go with pure T-SQL to avoid using functions.
A final note: You may not need the lower functions; my test database is case-sensitive, therefore the need to make the casing consistent.