Mysql Update Field Contents - mysql

I am currently trying to edit my db named boh. The current table "files" has a field called "path". Inside the path field is an actualpath to files listed in a folder, syntax "F:\xxx\xxx\xxx\filename.xxx". How do I update the field information to replace the "F:\xxx\xxx\xxx" so that just the file name exists?

It depends what you exactly want, if you want to strip constant path you can use:
UPDATE `table` SET `path` = REPLACE(`path`, 'F:\\xxx\\xxx\\xxx', '');
If you want to keep only last part after last \, then following command should do it:
UPDATE `table` SET `path` = SUBSTRING_INDEX(`path`. '\\', -1);

did you read this?
http://dev.mysql.com/doc/refman/5.1/en/replace.html

UPDATE files
SET path = REPLACE(path, 'F:\xxx\xxx\xxx\', '')
WHERE path LIKE = 'F:\xxx\xxx\xxx\%'
It's very easy to ruin your data with this massive updates so make sure you:
Try it first with a SELECT sentence
Backup your data

Assuming 'F:\xxx\xxx\xxx\' is not constant you could try a statement like this one:
UPDATE files SET path = REVERSE(SUBSTR(REVERSE(path), 1, LOCATE(REVERSE(path), '\')));

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 add http:// to all records in website column

I'm almost done transferring/reconstructing a substantial mysql db for new application. Column 'website' shows 'www.example.com'. Because the new application reads it as a hyperlink, I need the column to read 'http://www.example.com'. Is there a way to add the 'http://' in the beginning of each record for that column? Thanks in advance!
You can use the CONCAT function to do that:
UPDATE tbl SET website=CONCAT('http://', website);
If you want to get cleverer and only update columns which don't already have http:// prepended, try
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%';
Update
To prevent update to columns that have no site in them currently use this
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%' AND website<>'';
You can use the concat command. Something like
SELECT CONCAT('http://', website) FROM table
Use concat to generate the new column:
UPDATE table1 SET website = CONCAT("http://", website);

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 REPLACE variable string

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

UPDATE mysql database replace strings

I have in my db strings like www.domain.com and http://www.domain.com. I want to prepend to all entries the http:// but not affect other urls and as a result have this: http://http://www.domain.com
Can this be done with mysql only? I have used REPLACE(field,'www','http://www'), but this replaces also the http://www with http://http://www
Thanks in advance
EDIT
I forgot to mention that in the field there might be entries which don't contain www or http://www and therefore I don't want to alter or maybe there are entries like <p>domain</p> in which CONCAT() prepends the http:// before <p>
Try adding a WHERE clause to your update to only update fields that do not already have 'http://'. Test it out like this
SELECT CONCAT('http://', field) FROM foo WHERE LOCATE('http://', field)=0
and your UPDATE syntax would be:
UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0
I won't worry about performance as this seems like a one-off kind of script. That said, you can couple LEFT and CONCAT to achieve this:
UPDATE mytable
SET mycolumn = CONCAT('http://',mycolumn)
WHERE LEFT(mycolumn,7) <> 'http://'
Do note that I'm not taking CapItaliZation in to account. You may also want to consider sanitizing the information either before adding it to the database, or maybe make a trigger to do it for you.
Search and Replace Query - mysql replace
Here is the SQL query to replace string in your MySQL database table:
UPDATE table_name SET column_name = REPLACE(column_name,'original_string','replace_string')
Here is what I did to change the path URLs in all my previous posts.
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'http://localhost/','https://sureshkamal1.wordpress.com/')