How to UPDATE just part of a text entry in mysql? [duplicate] - mysql

This question already has answers here:
Replace a word in BLOB text by MySQL
(3 answers)
Closed 9 years ago.
I have a column in my database that has the absolute URLs to images.
I've just transferred the entire website to another folder so the URLs of the images have changed. So for eg. if the URL of an image in the image_URL column was like:
http://www.mysite.com/images/myimage.jpg
I need to update it like this:
http://www.mysite.com/newfolder/images/myimage.jpg
The type for the image_URL column is TEXT. But I need to update it ONLY if the URL being used is "mysite" and not "externalsite".
What's the right SQL to use? I'm very familiar with SQL UDATE commands but not where I need to update only PART of a column value.

UPDATE table SET image = REPLACE(image, "http://www.mysite.com/images/", "http://www.mysite.com/newfolder/images/")

Sure, just do a LIKE search for 'mysite' to get all the URL's. Then one by one, change the text and resave it to your dB.
lee

Related

How to add extra folder to multiple paths stored in MySQL database? From `Folder\file.jpg` to `Folder\New Folder\file.jpg`? [duplicate]

This question already has answers here:
Update a column value, replacing part of a string
(6 answers)
Closed 2 years ago.
Details:
I have stored physical paths into MySQL table. I have moved all content to a new folder.
This is the data in the database:
Current data---
g:\Folder1\File 1.jpg
g:\Folder1\Excel File.xlsx
g:\Folder1\Test.js
Desired change:
Here is what I'd like to achieve, add an extra folder to the path before the filename.
Desired---
g:\Folder1\New Folder\File 1.jpg
g:\Folder1\New Folder\Excel File.xlsx
g:\Folder1\New Folder\Test.js
Question
How can I achieve this? And just for future, how can I remove a specific folder from the path?
Simply use REPLACE as explained also in this SO question
UPDATE table
SET fied = REPLACE(field, 'g:\Folder1\', 'g:\Folder1\New Folder\')
It is untested so you may have to fix the \ escaping
Since REPLACE gives you the ability to replace the string with another one you can just modify the strings to "add or remove folders"
Use replace function:
SELECT replace(filename, "Folder1\\", "Folder1\\New Folder\\")
FROM your_table;
You can specify a path from drive letter to avoid random data being replaced.

SQL queries to replace wildcard text? [duplicate]

This question already has answers here:
MySQL string replace
(6 answers)
Closed 3 years ago.
so I have a small database I'm fooling around with and trying to get better at. I currently have a table filled with employee names and notes (just a mock up) so the result would look something like this:
Name / Employee # / Note
where it includes the employees name, their number, and a written note about the employee. I have written some sample text in the notes like "Is a great worker, never late - Adam"
SELECT * FROM `employees` WHERE `Note` LIKE '%Adam%'
I can use that to display all the notes that were posted by Adam, but lets say I want to change the name from Adam to Brad without having to rewrite every note, is there a way to replace that specific text but keep the rest of the note?
Thanks
You can use the following query:
update employees set Note = REPLACE(Note, 'Adam', 'Brad');
Check out this link for more info:
MySql Reference Manual

Modify str in db [duplicate]

This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have a table, and in one colu,m I store comma separated strings, like:
book, table, lamp (New)
need to write query loop through all the strings and remove
(New)
I think I can do it by first running a query to fetch results containing (New), then using PHP's REGEX, remove (New) and then update the the same row with new string. However this sounds a bit convoluted. Isn't there a way to do this with a single query?
There is a replace string function in mysql: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
UPDATE <table> SET <column> = REPLACE(<column>,'(NEW)','')
Try it with select first to see if the result is what you want (you may want to remove the trailing space if new is always preceded by it)

How can search for something and then change some text in table column by MYSQL [duplicate]

This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have some records in MYSQL database with domain name, Now i changed domain and i want use query for find these word example.com in record column and then change to example.net.
I think if column only include domain, it's very easy but my records include domain name and some text. Like this:
it's a sample text www.sample.com and i want change it
I have some recorde like above and i want find every recorde that have sample.com text and then change it to sample.net
You can use a simple REPLACE() for this:
UPDATE <table> SET <column> = REPLACE(<column>, 'www.sample.com', 'www.example.net');
The above will replace all occurances of www.sample.com with www.example.net in the specified column.
UPDATE mytable SET mycolumn = REPLACE(mycolumn,'www.sample.com','www.sample.net');
Replace

unable to insert the image in MySQL [duplicate]

This question already has answers here:
How to use LOAD_FILE to load a file into a MySQL blob?
(6 answers)
Closed 6 years ago.
I'm trying to insert the image in MySQL whenever i tried to add the image it showed null value. Pls help me to add the image. I've give the correct path but still error has occurred
create table image3(id int, image longblob not null);
insert into image3(id,image)values(571185, load_file('D:\Images\3.jpg'));
The function load_file() returns the string representation of the file (it only works with text files).
What you're trying to do is insert binary data.
EDIT I was wrong, disregard the answer, instead look at this one: Load_File doesn't work
EDIT 2 Could it be that you have to use / instead of \? I know windows uses \ for path separators, but it's quite frankly the only OS to do so. Therefore I can imagine that MySQL (being multiplatform) uses "the correct separators".