For example:
I have text in my column like: 'some text with word to replace' and i want to replace:
word with Word
i do:
update table set column = replace(column, 'word', 'Word');
and i get error:
Mysql: #1442 - Can't update table 'table' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
If you want to change only the first letter :
UPDATE MyTable
SET myColumn = CONCAT(UCASE(LEFT(myColumn, 1)), SUBSTRING(myColumn, 2));
If you want to change all the column :
UPDATE MyTable
SET myColumn = UPPER(myColumn);
If you want to replace some words, you have to use the replace function :
UPDATE MyTable SET myColumn = replace(myColumn, 'word', 'Word');
Please to consider to accept my answer if it's OK for you.
EDIT : Adding a third example to search and replace a word in the field and replace it to another one.
Use UPPER or LOWER functions in mysql.
Related
I need replace string1 to string2 in column big table. My table weight is1.7GB. For update I will use:
UPDATE table
SET column = REPLACE( column, 'search', 'replace' );
So, I have several questions:
Will table lock during procedure? Should I hide the public part of my site?
How long will this procedure be executing?
How better to run this? Maybe 'screen'? if my ssh connection will closed.
MySQL 5.5.53
You shouldn't run replace on all the rows of the table.
Instead add a where condition as #reds said in the comments.
This will make your update much more efficient and is the correct way to do it.
UPDATE table SET column = 'replace' where column = 'search';
In my case:
UPDATE table SET column=REPLACE(column, 'search', 'replace') WHERE column LIKE "%search%";
I have a database, a table that is having empty rows '' nothing in them.
I need to fill all those empty cells with certain singe value.
How can I do it?
Thanks in advance.
You can't batch edit all columns one column at each time
UPDATE table_name SET COLUMN_name ='value' WHERE column_name=''
If you want to update all columns at once, write a php code for that and in a loop perform sql query!
Hope it helps!
You have several options, based on your definition of "empty rows".
if empty means null:
update tableName
set columnName = 'value'
where
columnName is null;
if empty means not null, instead an empty string:
update tableName
set columnName = 'value'
where
columnName = '';
UPDATE table_name
SET column1=value
WHERE length(column1)=0;
This should do you job.
i need to replace only the domain of an URL present in many rows of a database with a different one. For example, the current URLs are something like www.myoldsite.com/index?s=something and i need to replace only the domain so they will look like www.mynewsite.com/index?s=something and so on. Is it even possible to do that in mysql?
I know how to update and set full values for all rows in a table having certain value but not only part of them with this:
UPDATE `mydatabase`.`mytable` SET `mycolumn` = 'http://mynewsite.com/' WHERE `mycolumn` = 'http://myoldsite.com/';
What can i do in this case?
Thank you.
Yes. Use the function replace():
UPDATE mydatabase.mytable
SET mycolumn = replace(mycolumn, 'http://myoldsite.com', 'http://mynewsite.com/')
WHERE `mycolumn` like 'http://myoldsite.com/%';
This isn't perfect, if the old site appears more than once in the URL.
Perhaps a better way is:
UPDATE mydatabase.mytable
SET mycolumn = concat('http://mynewsite.com/',
substr(mycolumn, length('http://myoldsite.com/')
)
WHERE `mycolumn` like 'http://myoldsite.com/%';
I think you're looking for something like
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%'
You should use REPLACE to replace the domain when you've found matches:
UPDATE mydatabase.mytable
SET `mycolumn` = REPLACE(mycolumn, 'http://myoldsite.com', 'http://mynewsite.com/')
WHERE `mycolumn` LIKE 'http://myoldsite.com/%'; -- wildcard on the end
The WHERE clause may be unnecessary here, but might be good for reference
you need to see -->
How can I use mySQL replace() to replace strings in multiple records?
so, you can update strings ...
UPDATE mydatabase.mytable SET mycolumn=REPLACE(mycolumn, 'old url', 'new url') where mycolumn='%old_url%'
you must be modified to fit the purpose.
I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.
Can we do it using Insert Query?
While looking for the answer I came across
UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'
So it is possible use Replace in Update query.
Can we do the same in insert query? Please Let me know if you can help me.
You can use the following:
REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)
More info from:
http://www.mysqltutorial.org/mysql-replace.aspx
You can use a BEFORE INSERT TRIGGER on your table
CREATE TRIGGER trigger_name
BEFORE INSERT
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);
How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?
To update a single column in a single table, you can use a combination of LOWER() and REPLACE():
UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))
To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:
ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name;
UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_'));
Just be sure to update the data-type in the ALTER command to reflect your actual data-type.
When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D
My Answer though:
REPLACE(string1, find_in_string, replacementValue);