How can I replace the value(unknown length) of a column in mySQL workbench with a substring that exists in that column?
For ex:
If I have the value of a column like "ABC.123.Chrome/123", how do I replace this for all rows with just "Chrome/123"? I want to replace value in that string of unknown length with everything that comes after Chrome only.
This should work if you want to include Chrome:
UPDATE MyTable
SET ColumnName = SUBSTRING(ColumnName,LOCATE('Chrome'))
WHERE ColumnName LIKE '%Chrome%'
Related
i have a string with the following value
Germany,Sweeden,UAE
and i have an mysql row that have a value of one of this separated string with comma
as example a Country Row with a value Sweeden .
i have tried to use this sql statement to update a targeted row where its value is like one of this string separated with comma
hence if the row have a value of sweeden it should be updated sense sweeden value is include in the comma string
UPDATE CountryList SET Time= '100' WHERE Country LIKE '%Germany,Sweeden,UAE%'
but i couldn't update the targeted row using this syntax. any idea what is the correct syntax i should use ?
I think you want find_in_set():
update CountryList
set time = 100
where find_in_set(country, 'Germany,Sweeden,UAE')
This checks if country matches any of the values in the comma-separated row given as second argument to find_in_set().
Note that I removed the single quotes around the literal 100: if time is a number, then treat it as such (if it's a string instead, then keep the quotes).
I have a question in mysql about searching the column in a table and if it contains specific string replace whole column with particular text.
Lets say If the column containing this textdata/variant_image/2/IW_HH_1B_sxph-2o_l7ar-4r_kh2k-fd.jpg has this string in itIW_HH_1B_ then replace it with data/variant_image/1/IW_HH_1B.jpg
I tried update,replace functions but it didnt work. And searching on google returns only replacing that particular string option. but not complete column text.
A simple update works:
UPDATE your_table
SET your_column = 'data/variant_image/1/IW_HH_1B.jpg'
WHERE your_column LIKE '%IW_HH_1B_%'
You can use a conditional update for that:
UPDATE tablename
SET columnname = 'data/variant_image/1/IW_HH_1B.jpg'
WHERE LOCATE('IW_HH_1B' COLLATE utf8_bin, columnname) > 0
If you want it to be case insensitive leaf the collate part out.
Im trying to update a field only if the field contains certain characters j,k,l,m,n,o,p,q,r
I want to avoid using update tbl set field = field2 where field like '%j' or field like '%k' ...etc.
Is there some syntax to create a list of the characters I am looking for?
Try this in MySQL:
update tbl set field = field2 where field REGEXP 'j|k|l|m|n|o|p|q|r';
You can use regular expressions with the RegExp keywork.
I have a database table in MYSQL with around 1000 rows. In the table I have a column called 'overview'. In each row, this column has some value and at the end of that value I have a specific line (text) starting with: 'Source...'
Now what I want is, I want to remove this line from each column and replace it with some other text content.
I believe it can be accomplished with some smart query.
You can simply use REPLACE in your query like this
UPDATE your_table SET col_name = REPLACE(col_name , ‘Source...’, ‘new_val’)
WHERE col_name LIKE '%Source...';
Check Out the SQLFIDDLE.
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string).
The syntax of REPLACE is:
REPLACE (text_string, from_string, to_string)
In your case, you can do this way:
UPDATE `tableName` SET `column` = REPLACE(column , 'Source...', 'Replaced Value')
Use Replace
update TBL
set overview = Replace(picture, 'Source..', 'replacement')
keep a backup of the table before anything.Or you can do it on a copy.
you can do this by following:
update table_name set col_name = replace(column_name , ‘Source...’, ‘Replaced String...’);
I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?