MySQL REPLACE variable string - mysql

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??

USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))

Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.

I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).

first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')

try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

Related

MySQL Remove part of string after specific character

I do got a database named "MAPPINGS" with about 23.000 rows I need to replace becouse of a mistake. Is there any query to achieve the following:.
UPDATE MAPPINGS SET MAIN = 'firt part' WHERE USERID = '1578' AND MAIN LIKE 'first part >%'
The problem is that "first part" is every time something else. I just need to remove everyhing after the ">" than the MAPPINGS are correct.
Or can this only be done by a PHP script? while select * from mappings where userid = '1578' and then the update query. I hope there will be a query to achieve this.
You can do this in MySQL using substring_index():
UPDATE MAPPINGS
SET MAIN = SUBSTRING_INDEX(MAIN, '>', 1)
WHERE MAIN LIKE '%>%' AND USERID = '1578';

MySQL update to change all instances containing -html to .html

I need to update a bunch of records in my database that store a slug in an article table. I mistakenly set the slugs to end in "-html" rather than ".html" and I need a query that will fix this.
I don't really understand how to use variables, so I'm hoping someone here can help.
Would someone please write for me a SQL query that's something like:
UPDATE table
SET table.slug = '%.html%'
WHERE table.slug LIKE '%-html%'
Obviously, that's not correct, but I don't know the correct way to write it.
Here's a quick-and-dirty example using REPLACE()
UPDATE table
SET slug = REPLACE(slug, '-html', '.html')
WHERE slug LIKE '%-html'
Just be warned that this will replace any occurrence of -html, even if it's not at the end of the string.
A more comprehensive approach might be
UPDATE table
SET slug = CONCAT(TRIM(TRAILING '-html' FROM slug), '.html')
WHERE slug LIKE '%-html'
Mine will only replace the last '-html', and append '.html'by CONCAT:
UPDATE table
SET slug = CONCAT(SUBSTRING(slug, 1, LENGTH(slug) - 5), '.html')
WHERE slug LIKE '%-html'
You have to make use of replace command.
UPDATE Table Tablename
SET MyColumnname = REPLACE(MyColumnname, '-html', '.html')
WHERE MyColumnname LIKE '%-html%'
I'd make use the the TRIM and CONCAT functions:
Something like this:
UPDATE `table` t
SET t.slug = CONCAT(TRIM(TRAILING '-html' FROM t.slug),'.html')
WHERE t.slug LIKE '%-html'
Note that the TRIM(TRAILING '-html' will remove all occurrences of that specified string from the end of the column value, so if I had (for example) a column value of 'foo-html-bar-html-html', that would return 'foo-html-bar'.
I use the CONCAT function to append '.html'.
The WHERE clause guarantees that I will only be modifying rows that have a column value ending in '-html'.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html

MySql Add onto an exisiting entry in the database

Just wondering is it possible to add onto an exisiting entry in the database without having to do a query to look whats in there?
Ive done it with numbers before like
UPDATE table SET views = views +1
But is there a way to do it with a string?
Thanks
UPDATE table SET views = CONCAT(views,'some text to add on to it')
What you are looking for is CONCAT() function:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
UPDATE table SET field = CONCAT(field, "some str" ) WHERE id = 123;

Changing column content in many rows

I have a column called code and in every row, the column contains FE. Because I do not want to go through 13,000 records, is there a quick way to replace FE inside 'code' with FEU?
While I appreciate this may be a simple question (or not?), I wasn't sure how to word it in order to find a solution.
You should be able to do like this:
UPDATE <table> SET code = REPLACE(code, 'FE', 'FEU');
If the column really just contains the value FE a simple WHERE clause should be enough:
UPDATE <table> SET code = 'FEU' WHERE code = 'FE';
Maybe something like this?
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'
This will work if your column contains only the string 'FE' and you want to replace it with 'FEU'.
update `table`
set `code` = replace(`code` , "FE","FEU")
where (if there is a where write it here)
should do you
This is probably the quickest way. You can try this:-
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'

mysql update statement path links adjustment

I got table smile need to update smilepath ( mass update ) im using this
UPDATE `smilie` SET `smiliepath` = 'http://newpathhere.com/smilies/smile.gif' WHERE `smilieid` =1;
ive to issues ,
need to mass update to all paths , i believe i can use where smileid > 1 not quite sure
2nd issue in old path its looks like http://oldpath.com/icons/smile.gif
so i need to replace only " http://oldpath.com/icons " and keep end file
any clue !
You can use mysql REPLACE function to do this:
UPDATE `smilie` SET `smiliepath` = REPLACE(smiliepath,' http://oldpath.com/icons/','http://newpathhere.com/smilies/') WHERE `smilieid` >1;