one of my table contains column path stores the URL example:\xyz\attachments, \abc\attachments, etc total i have 16 combinations to replace
i found rows by using rlike in where clause 'abc|xyz|'
have to update xyz with xxx or abc with yyyy
i am not sure how to update these part of the values of column. Is it possible using single query or i have to write 16 queries to do that? please advise here
This is not reliable, but is doable. Basically nested replace() calls:
UPDATE ...
SET yourfield = REPLACE(REPLACE(yourfield, '\\xyz', 'newtext'), '\\abc', 'othertext')
Note that if xyz or abc can appear in multiple places in either string, you may end up replacing something that shouldn't have been.
Related
I'd like to use an SQL query to find and replace multiple values. I've had a look at this question that shows the following answer:
UPDATE
YourTable
SET
Column1 = REPLACE(Column1,'a','b')
WHERE
Column1 LIKE '%a%'
How can I find and replace multiple values instead of just the one?
My data is like the following, there's hundreds of rows, I'm specifically wanting to target each product_id:123:
subscription_id,products
"128","product_id:268|quantity:1|total:3.15|meta:|tax:0;product_id:267|quantity:1|total:2.97|meta:|tax:0"
I need to replace the product id's with new products id's. So it'll be "everything matching 268 will become 195" and "everything matching 267 will become 194".
Is there an efficient way to do it other than taking the code block above and using that for each product. Can I be done with one sweep through?
Simplest possible way would be to chain REPLACEs together, but considering the concatenated nature of the field you need to be sure you don't inadvertently target something that's not actually a product_id value. You can mitigate this by including some contextual content from the string value itself:
UPDATE YourTable
SET products = REPLACE(REPLACE(products, "product_id:267|", "product_id:194|"), "product_id:268|", "product_id:195|");
DBFiddle | MySQL 5.6 Reference Manual :: 13.2.8 REPLACE Statement
If there's some variability in how these strings might appear in a given field and you're running MySQL >=8.0, you can leverage something like REGEXP_REPLACE() to perform this same replacement using a defined RegExp pattern.
Yes, there are ways. For example, you can create a table like
replacements(id, oldval, newval)
and do the following:
UPDATE
Yourtable
JOIN
replacements
ON
Yourtable.Column1 LIKE CONCAT('%', replacements.oldval, '%')
SET
Yourtable.Column1 = REPLACE(Yourtable.Column1, replacements.oldval, replacements.newval);
The problem is that you would need to fill replacements with the pairs of oldval-newval, but MySQL cannot guess that. Insertion is as simple (assuming that id is auto_increment) as
INSERT INTO replacements(oldval, newval) VALUES
('a', 'b'),
('c', 'd'),
...
;
id text_1 text_2
1 おはよう おはよ
2 こんにちは ちわー
3 大丈夫 さよなら
4 でんわしたい でんわしよう
I have DB same above.
I want to search with input: おはよう大丈夫?
Expect result will match: id = 1 and id = 3.
Please help me how to query search in Mysql? Thanks you.
The following SQL query will fetch your 2 ids for exact string matching
select id from TABLENAME where text_1 in ('おはよう','大丈夫');
You can also use like operator with % to fetch approximately strings id.
You might face encoding issue (文字化け)depending on your DB settings, so you might have to convert SJIS <-> UTF-8
https://dev.mysql.com/doc/refman/5.7/en/faqs-cjk.html#faq-cjk-what-cjk-avail
Last but not least, if you want to use the full string as a comparison criteria to select the rows, then you can reuse the following code:
how to compute similarity between two strings in MYSQL
select id from TABLENAME where text_1 REGEXP "おはよう大丈夫"
eg : field name = User_id
Value=abc later i want to insert xyz without disturbing abc Value= abc,xyz i want to insert efg without disturbing abc then Value= abc,xyz,efg and so on
i want to seperating each value by using ","(comma). can any one help me out
In MySQL you could often refer to the value of a column just by using the column name. And to concatenate strings with a separator there's a nifty function called concat_ws (concat with separator).
In your case the code would look something like
UPDATE YourTable
SET Value = CONCAT_WS(',', Value, 'cde')
WHERE User_id = 123;
Good Luck!
MySQL CONCAT_WS() function is used to join two or more strings with separator. The separator specified in the first argument is added between two strings. The separator itself can be a string. If the separator is NULL the result is NULL.
Click hear for more information
I have a very large MySQL table with lots of data in it, one of the fields is Invoice No, and is a number starting at 1000.001 (This is a string). I have got this from someone that left the company and they imported the data through excel and some of the numbers have come across as 1000.01 instead of 1000.010.
When I run this query in php my admin, it shows there are over 11k rows, so I can see them ok.
SELECT `AnalysisID` , `InvoiceNo`
FROM `STStbl000010`
WHERE `InvoiceNo` LIKE '%.__'
ORDER BY `STStbl000010`.`AnalysisID` ASC
So simply put I need to add a 0 (Zero) to the end of those entries.
I have tried the following, however, it just returns 0 rows effected.
Can I use wildcards like this in and Update and Replace Statement?
UPDATE `STStbl000010AT`
SET `InvoiceNo` = replace(`InvoiceNo`, '%.__', '%.__0')
WHERE `InvoiceNo` LIKE '%.__'
Thanks
You can't use wildcards in a replace, hence no matched rows.
Luckily, if you just want to add a 0, you can concatenate the string values:
UPDATE `STStbl000010AT`
SET `InvoiceNo` = CONCAT(`InvoiceNo`,'0')
WHERE `InvoiceNo` LIKE '%.__'
Very simple
I have a column of string like that :
abc1
abc2
abc3
abc4
abc5
I need to remove 'abc' from this column and keep only the number on the right, so column would have only the number like :
1
2
3
4
5
I thought about smth like that but it doesn't work
update table2
-> set col1 = case when 'abc1' then 1
-> else end;
I know how to concat text, I don't know how to undo it... Help please ^^;
#McArthey already hinted at it, but this is easy to do when the "abc" is consistently "abc" (i.e. the length doesn't change.)
Amongst the various string functions is one in particular: RIGHT(). This allows you to select a fixed number of characters from a string. e.g.
SELECT RIGHT('abc3',1) -- Results in "3"
SELECT RIGHT('abc3',2) -- Results in "c3"
Coupled with the LENGTH() function, you can conclude the numbers are anything past the 3rd character. i.e.
SELECT RIGHT('abc3',LENGTH('abc3')-3) -- Results in "3"
Obviously I'm using hard strings ('abc3'), but these can easily be replaced with column names.
The caveat here is that these all are based on fixed length letter prefixes. The more variable (changing) the "abc" in your example is, the harder picking the numeric value out of the column becomes.
If these are single digit values you could use
select right(column,1) ...
You may also find the REGEXP docs useful if it is more complex.
If you are trying to modify the column you will have to take the values separately and then concatenate them back together. It's difficult to give a precise answer since I don't know what you're trying to accomplish but you could do something with SUBSTR to grab the separate values.
Get 'abc': SUBSTR(column, 1,3)
Get digits: SUBSTR(column, 4)