SQL Replace some text of each string of a column - mysql

I have a database table that contains a column named: "shortLink".
This column contains a short link of an address of each row in the table.
I use the tinyurl.com services for the short links. The short link looks like this: https://tinyurl .com/randomletters.
Recently I figured out that I need to change the shortlinks to their preview short link version: https://preview.tinyurl .com/randomletters.
The only difference between the two link formats is that there is a prefix preview. between https:// and tinyurl.
Since I have hundreds of rows in the sql table I cannot fix this manually.
Is there any way to convert each shortlink (by adding the prefix preview. in the address) to its preview format with code in sql?
Thanks.
PS - Notice that there is a gap between tinyurl and .com in the link formats above. This gap is added intentionally because the forum would not let me publish the question otherwise.

-- this will update the field for you where it does not already have the preview. in it.
UPDATE YourTable
SET shortlinks= REPLACE( shortlinks, 'https://tinyurl .com', 'https://preview.tinyurl .com')
WHERE shortlinks NOT LIKE 'https://preview.tinyurl%'

You can just use an update:
update t
set shortlink = concat('http://preview.', substring(shortlink, 8))
where shorlink like 'http://tinyurl%';

Bro try this. first, remove all spaces in URL on anywhere then replace 'tinyurl' to
'preview'
UPDATE [Table_Name]
SET shortLink= REPLACE(REPLACE( shortLink, ' ', ''),'tinyurl', 'preview')

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.

How can i generate url with hyphens from title column in mysql table

I have a mysql table with two columns: Title and url. The url should be from title, and can be created by removing spaces and all characters including spcial characters, then replacing thme with hypens (-)
For example
If the title column is: Tell us more about your #project"
Then Url column becomes: Tell-us-more-about -your-project
Somebody please tell me an sql statement i can use to do that
I just need help for anyone willing to share the knowledge. Please post in ur proposal so i can try
update tb set url=replace( replace(title,' ','-'), '#','-')
in your case will do the job but in general you have to braket
multiple time the internal replace with the new character to be replaced :
replace( (expr) ,'(special char)','-')
You need php str_replace and strtolower or you use Apache Module mod_rewrite

How to remove double slashes from column in MySQL?

I have recently found a bug in my website that has been inserting an extra backslash in the URL. I have fixed the bug, but would like to clean up my historical data.
My table name is visits and column name is url. Some of the example URLs in the field are:
www.mysite.com//
www.mysite.com//restaurant/mcdonalds/
www.example.com//
How can I do a find and replace on // to change it to / in MySQL?
I would greatly appreciate any help. Thanks!
update visits set url=replace(url, '//', '/');

mysql to update a database using UPDATE SET and TRIM(LEADING wildcard prefix in record

In my database I have a table called 'content' and a field called 'link' and there are almost 300,000 records in that table.
In the field called 'link' there are a number of records that look like this :
http://www.example.com/blah/blah/123456789/url=http://www.destination.com
Unfortunately the prefix part of the records are individually unique where the numbered portion is constant changing from 90 to 150 alpha-numeric characters
I would like to remove the prefix up to and/or including the url=
So that the only thing left in the record is :
http://www.destination.com OR
I could even work with
url=http://www.destination.com
and simply do a replace command against the "url=" part as a second mysql command.
If there was a wildcard command, this job would be much easier and I would just wildcard everything showing up in the link record between :
http://www.example.com/blah/blah/ wildcard url=
But as everyone knows... there is no such wildcard available
So it had me looking at the UPDATE, SET and TRIM(LEADING commands
UPDATE content
SET link =
TRIM(LEADING 'url=' FROM link)
But this DID NOT generate the changes I wanted
And so I took the labor intensive method of downloading the database and using a Search and Replace program to make the changes to the 44 thousand records that contained these parameters.
But I would love to find a command that I could simply pass to the database to make this simpler in the future.
Any thoughts on how to accomplish this change in the future would be greatly appreciated.
Thanks in advance ;
You can use the SUBSTRING_INDEX function:
UPDATE content SET link=SUBSTRING_INDEX( `link` , 'url=', -1 )
I have not tested it, so I would recommend you check that substring_index returns the desired string first.
Assuming that the part you want to keep always begins with 'http://' you could get the desired result string with the help of the SUBSTRING_INDEX function:
SELECT CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1)) FROM content;
and fix your table with the simple statement
UPDATE
content
SET
link = CONCAT('http://', SUBSTRING_INDEX(link, 'http://', -1));
Explanation:
SUBSTRING_INDEX with third parameter negative returns the substring from the last occurence of the needle in the second parameter to the end. Because 'http://' isn't included in the return value, we add it again.
Remark:
If you've got https:// urls too, you should be able to adapt my solution.

Removing an Appended " In front of Every Column

I found a CSV database of Cities/ZIP/GPS, and when I imported it, it added a " infront of the columns.
alt text http://www.grabup.com/uploads/58754a865eebd94c9aafaf7444b52d15.png?direct
I don't want to go in for 33,000 entries and do this manually, is there a query I can run that will remove the quotes?
i'm not a MySql expert but this should work: (based on my similar experience in Sql Server)
UPDATE table_name SET col_name = REPLACE(col_name, '"', '')
For more info on the REPLACE and other string parsing functions, see here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
in sql server you coud do:
update mytable set state= substring(state,2,29)
change the "29" to whatever the actual length is.
I am sure mysql must have equivalent syntax.
Repeat for each field, it looks like there is only a handful of them.
As an alternative you could filter the original csv document - isn't that easier?