Replace the text of whole column it contains specific string mysql - mysql

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.

Related

How to delete specific value from a column in SQL row?

I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?

Creating new column using keywords

I'm looking to create a new column with values that correspond with keywords in an existing column in mysql.
I'm trying to determine if a practice is private or public based on keywords in their establishment's name.
In plain english what i'm trying to get is a new column called Practice and insert value private if keywords are specialist, gleneagles.
Would appreciate any help on this matter.
If I understand what you're asking, you want to add a column to an existing table and then set that column's value (for each row) based on another column's value in that same row?
First, to create the column named Practice you can use MySQL's ALTER TABLE command:
ALTER TABLE YourTable ADD Practice VARCHAR(20);
I'm not sure what type you are aiming for here, so I'm just going with VARCHAR(20); you may also benefit from an enum based on the values you're using (or even a foreign-key to a lookup table).
After you've created your column, you can set its value:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn = 'specialist' THEN
'private'
WHEN SomeColumn = 'gleneagles' THEN
'private'
ELSE
'public'
END
If you don't want records that don't match to be set, simply drop the ELSE 'public' and they'll be defaulted to null.
UPDATE (to select with wildcards)
If you need to update rows with the related data being contained within larger text and you need wildcards, you can use MySQL's LIKE operator:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn LIKE '%specialist%' THEN
'private'
WHEN SomeColumn LIKE '%gleneagles%' THEN
'private'
ELSE
'public'
END
Alternatively, if you're only going to update to a single-value based on text containing multiple values, you can use a Regular Expression (via REXEXP) instead:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn REGEXP 'specialist|gleneagles' THEN
'private'
ELSE
'public'
END
You are asking about denormalization, which means storing derived data. My initial reaction would be to not add another column, unless there were significant performance problems, instead I would create a view that gas the calculated value.
However, if you need the column, I would create a boolean column:
alter table mytable
add is_private boolean;
Then populate it like this:
update mytable
set is_private = (name like '%specialist%' or
name like '%gleneagles%')

Adding text to each column of MYSQL Database

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...’);

Determine CaseSensitive in WHERE clause

for example TableColumn could be contains value in forms New, new or NEW, how can I write query that returns only
SELECT * FROM myTable
WHERE myColumn = 'New'
but doesn't returns TableRows contains new or NEW
For MySQL, a simple option is:
SELECT * FROM myTable
WHERE myColumn = 'New'
AND BINARY(myColumn) = BINARY('New');
The second condition is logically sufficient, but makes the query slow if the table is big (the Index on myColumn cannot be used). The combination of the 2 conditions allows index use for the first condition, and then filtering out the non matching case.
You can use COLLATE in your where clause
SELECT *
FROM myTable
WHERE myColumn COLLATE latin1_general_cs = 'New'
The Best Way to make this column case sensitive is change this particular column if charset is UTF8 change it's collation to collate utf8_bin, after the modification of column it search case sensitive.
i.e I have a table name people with column name "name".
alter table people
modify column name varchar(50) charset utf8 collate utf8_bin;
Note : You can use varchar data type to varbinary, it works fine..
SELECT * FROM users WHERE BINARY userid = 'Rahul';
mysql does not check case of word so in the case of username or userid we shouold need to check case also for more security.
I hope this will help you

replacing value of a column(unknown length) with substring

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%'