MySQL Update if field contains certain characters - mysql

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.

Related

UPDATE tbl SET name = REGEXP_REPLACE(`name`,[:digit:],'')

I try to get rid of some digits in my table.
What is wrong with this:
UPDATE tbl SET `name` = REGEXP_REPLACE(`name`,[:digit:],'')
it won't execute showing: REGEXP_REPLACE is not valid at this position...
thanx.
You put the field between comma: 'name'. Your query is trying to REGEXP replace inside the text 'name'. Rremove the quotes to replace your field value:
UPDATE tbl SET name = REGEXP_REPLACE(name,[:digit:],'')

Add character after a value sql

I need of add character after a value sql field in auto mode. Example i have numeryc type fields 01 and i wanna add after the number PC so trasform the value in 01PC.
help me?
This query add the letter A at the end of field1
select concat(field1, 'A') from table1
And if you wanna update the table you can do..
update table1 SET field1 = concat(field1, 'A')
The last query add letter A at the end of all field1
Use the convert sql function like so
select concat(convert(nvarchar([length]), field1), 'pc') from table1

mySQL remove all text from record except keyword

I'm basically looking to remove all of the text from a record called FaxOutNumber except for where it contains no... it isn't consistent in the records, so sometimes its just NO! and other times its no#emailaddress.com.
I'd like:
FaxOutNumber:
5145555#emailaddress.com
no!#emailadrress.com
to change to:
FaxOutNumber:
[null]
no
I'd actually like to just turn this field into a simple BIT where the "No" becomes a "1" or true value as well.
Thanks in advance!
You can use a regular expression:
ALTER TABLE my_table ADD COLUMN isNoRecord BOOLEAN;
UPDATE my_table SET isNoRecord = FaxOutNumber RLIKE '^no(!?)(#.+)?$';
ALTER TABLE my_table DROP COLUMN FaxOutNumber;
You can try this--
UPDATE tableName SET FaxOutNumber = IF(FaxOutNumber RLIKE '^par',"",FaxOutNumber);

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

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